str = fileread('test.txt');
N = sscanf(str,':MSR %d');
[S,E] = regexp(str,'(?<=^# Measurement number).*?^:EOM','lineanchors');
assert(all(N==[numel(S),numel(E)]),'This file is incomplete or corrupted.')
out(N) = struct();
for n = 1:N
sub = str(S(n):E(n));
out(n).num = sscanf(sub,'%d');
tkn = regexp(sub,'^%(\w{3})([^#]*?)(#\s.*?)?\s*$','lineanchors','tokens');
for m = 1:numel(tkn)
tmp = tkn{m};
out(n).(tmp{1}) = strtrim(tmp{2});
if ~isempty(tmp{3})
out(n).([tmp{1},'_note']) = tmp{3}(3:end);
end
end
tmp = regexp(sub,'^#(\s+\w+)+\s+#\s+=','tokens','lineanchors','once');
out(n).hdr = regexp(strtrim(tmp),'\s+','split');
tmp = regexp(sub,'^=\s+(\s+[\d\.-]+)+\s*$','match','lineanchors');
out(n).dat = cell2mat(textscan([tmp{:}],'=%f%f%f%f'));
end
Best Answer