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 Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by