Read txt file in Matlab

2019-01-25 15:59发布

问题:

I have a trouble reading the txt file, which contains 10 columns and 2 lines of header, but the problem is that in the middle of the file the same header appears several times and textread() doesnt function. That's my file example:

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 the error exactly at the row with the repeated header. Do you have any ideas how could I avoid those headers and read the complete file? The problem is that I have hundreds of these types of files, so I cant delete each time manually.

Thanks for help.

回答1:

You can first read the file line by line with textscan taking the whole line as a string. Then remove the headerlines, 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


回答2:

If you do NOT want to use perl, awk or something like it to preprocess your data (which I actually could really understand), you could try to read your file line by line by using fopen, fgetl and feof (e.g. one example can be seen here: https://stackoverflow.com/a/2858208/701049) and check for each line if it contains a header. If so, continue your loop. If not, process it by using something like textscan as you do now already.