Read txt file in Matlab

陌路散爱 提交于 2019-11-29 07:02:25

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
tim

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!