How to open text file with headers through matlab code?

조회 수: 17 (최근 30일)
Marine Sachel
Marine Sachel 2016년 3월 28일
답변: Star Strider 2016년 3월 29일
I have a text file (attached here) which contains 2 header lines in the beginning and the same headers appear several times in the text file. I need to open this file i.e. 'ppppprofile120.txt' skipping all headers via the matlab code.
I tried opening the text file after removing the headers in only the first two lines of this data manually, through the code mentioned below:
fid = fopen('profile1200.txt', 'r');
Data = textscan(fid,'%s','delimiter','\n');
Data_new=[Data{:}];
N={};
N{end+1}=Data_new;
fclose(fid);
NB=[N{:}];
NB(1:155:end,:)=[];
NB(1:154:end,:)=[];
and it executed but I have copious amount of files so that will be laborious for me to remove their headers manually one by one.
Can anyone help me opening this file with or without headers via code?
  댓글 수: 1
Marine Sachel
Marine Sachel 2016년 3월 29일
Data = textscan(fid,'%s','delimiter','\n','HeaderLines',2)
@ Azzi Abdelmalek: I tried the code I have mentioned above but it gave me an empty data cell of {0 x 1}

댓글을 달려면 로그인하십시오.

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 3월 28일
Data = textscan(fid,'%s','delimiter','\n','HeaderLines',number_of_lines_to_skeep)

Image Analyst
Image Analyst 2016년 3월 29일
Use importdata():
filename = 'ppppprofile120.txt'
dataStructure = importdata(filename)
headerLines = dataStructure.textdata
numericalArray = dataStructure.data

Star Strider
Star Strider 2016년 3월 29일
This approach has worked with previous files with header lines within the file that I’ve successfully used textscan to read, but it doesn’t work with the file you posted because there is no end-of-file marker. It cuts off in the middle of one line, and that screws up everything.
Anyway, the code for the complete file (with the end-of-file marker) would be:
fidi = fopen('Mehak Jabbar ppppprofile120.txt','r');
k1 = 0;
while ~feof(fidi)
k1 = k1 + 1;
D{k1} = textscan(fidi, '%f%f%f%f%f', 'HeaderLines',2, 'Delimiter','\n', 'CollectOutput',1);
fseek(fidi,0,0); % Position Start Of Second Part Of File
end
fclose(fidi);
A version of this works for the first 8 sections (that you attached in part), so I know it will import all but the first two lines in each section. Note that the second line in each section has a string at the end of the line, so since there appears to not be any way to allow for a string at the end of all the lines, it is necessary to discard it in the textscan call.
It would be necessary to run the loop again, this time with:
D1{k1} = textscan(fidi, '%f%f%f%f%f%s', 'HeaderLines',1, 'Delimiter','\n', 'CollectOutput',1);
to pick up those ‘second’ lines. There are other ways, but this is (paradoxically) the easiest. You would then have to insert those lines in the appropriate rows of your matrix, but considering that you have several different cells, that would definitely be inconvenient but should not be terribly difficult.

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by