MATLAB: How to read line with multiple separate headers and tabs in between values

data importMATLABtext filetextscan

So I was wondering about the following:
I have a simulation export from LTSpice with the following structure:
time V(vin) V(vout) I(B1) I(D1) I(Rload) I(V1)
Step Information: D=200m (Run: 1/13)
0.000000000000000e+000 1.291820e+001 1.247644e+001 2.187000e+000 2.186138e+000 2.970581e+000 -7.852747e-001
7.902717741844912e-011 1.291820e+001 1.247649e+001 2.187000e+000 2.188567e+000 2.970593e+000 -7.836857e-001
8.444024477795836e-011 1.291820e+001 1.247649e+001 2.187000e+000 2.188719e+000 2.970593e+000 -7.835863e-001
9.165766792397068e-011 1.291820e+001 1.247650e+001 2.187000e+000 2.188895e+000 2.970594e+000 -7.834706e-001
.
.
Step Information: D=250m (Run: 2/13)
(More data, same structure)
I am unable to read all lines as a string and keep the formatting intact, most likely due to the tabs that separate the data values. Is there a way to replace tabs by spaces with the `textscan` function or a way to keep the formatting?
clear all;
fid = fopen('s11.txt','r');
S = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '', 'TreatAsEmpty', '');
S = S{1};
fclose(fid);
% Find subheaders
idx = find(contains(S,'Step'));
cycle_data = cell(length(idx),1) ;
for i = 1:length(idx)-1
cycle_data{i} = S(idx(i)+1:idx(i+1)-1);
end
cycle_data{end} = S(idx(end)+1:end) ;
Which yields a matrix with 13 entries (all cycles/data subheaders separated) but the problem I get is that the data values are concatenated. More concise:
In an entry I get lines like this:
0.000000000000000e+0001.291820e+0011.247644e+0012.187000e+0002.186138e+0002.970581e+000-7.852747e-001
Instead of what I want:
0.000000000000000e+000 1.291820e+001 1.247644e+001 2.187000e+000 2.186138e+000 2.970581e+000 -7.852747e-001
Thanks in advance!
Ps. An alternative solution is also completely fine for me.

Best Answer

It would help to have your actual file.
What information do you want to import? Obviously you want the matrix data, however do you also want the ‘Step Information:’ line?
If you want all of that information, rather than using textscan, it would likely be easier to use fileread to read the entire file as text, then extract what you want.
If you only want the matrix data, that is relatively straightforward to get. See for example: How to skip lines of Data in a middle of a text file, with this example reading some section header character arrays as well.