Reading txt file in matlab

I have a problem reading a txt file that contains 10 columns and 2 header lines, but the problem is that in the middle of the file the same header appears several times and textread() does not work. An example of my file:

file.txt

 headerline1 aaaa headerline2 111 123 20/12/2000 name1 name2 name3... name8 0 21/12/2000 name1 name2 name3... name8 0 22/12/2000 name1 name2 name3... name8 0 headerline1 aaaa headerline2 111 123 25/12/2000 name1 name2 name3... name8 0 27/12/2000 name1 name2 name3... name8 0 ... 

and this is my code i tried:

 [date, name1, name2, name3, name4, name5, name6, name7, name8, status] = ... textread('file.txt', '%s %s %s %s %s %s %s %s %s %d', 'headerlines',2); 

It gives an error exactly on the line with the re-header. Do you have any idea how I can avoid these headers and read the full file? The problem is that I have hundreds of these types of files, so I can’t delete it manually each time.

Thanks for the help.

+6
source share
2 answers

First you can read the file line by line textscan , taking the entire line as a line. Then remove the headers and process the rest

Here is an example:

 %# read the whole file to a temporary cell array fid = fopen(filename,'rt'); tmp = textscan(fid,'%s','Delimiter','\n'); fclose(fid); %# remove the lines starting with headerline tmp = tmp{1}; idx = cellfun(@(x) strcmp(x(1:10),'headerline'), tmp); tmp(idx) = []; %# split and concatenate the rest result = regexp(tmp,' ','split'); result = cat(1,result{:}); %# delete temporary array (if you want) clear tmp 
+6
source

If you DO NOT want to use perl, awk or something like that to preprocess your data (which I really could understand), you can try to read your file line by line using fopen , fgetl and feof (for example, one example can be seen here: fooobar.com/questions/299494 / ... ) and check each line if it contains a title. If so, continue your loop. If not, process it using something like textscan as it is now.

+2
source

Source: https://habr.com/ru/post/907950/


All Articles