It looks like you can look for the line with '[data]' on it, and continue until the first line that doesn't start with a digit or a minus sign.
The code below takes the scenic route, but it gets there in the end. If you have a very large number of files you may want to consider a more efficient method.
A=readfile('https://www.mathworks.com/matlabcentral/answers/uploaded_files/348641/IN01_SCALED.txt');
n_start=0;n_end=-1;
for n=1:numel(A)
if strcmpi(A{n},'[data]')
n_start=n+1;
end
if n_start>0 && n>=n_start
if ~any(A{n}(1)=='-0123456789')
n_end=n-1;
break
end
end
end
B=A(n_start:n_end);
B=cellfun(@(x) cellfun(@str2double,strsplit(x,',')),B,'UniformOutput',0);
B=cell2mat(B);
Let's stretch the definition of what an earlier release is. I made the above code compatible with just about any Matlab release. I didn't test it, but it should work with releases as far back as R13 (version 6.5). For the modern code I used the solution by Star Strider.
UseFallbackMethod=true;try if ~verLessThan('matlab','9.6'),UseFallbackMethod=false;end,catch,end
if UseFallbackMethod
A=readfile('https://www.mathworks.com/matlabcentral/answers/uploaded_files/348641/IN01_SCALED.txt');
n_start=0;n_end=-1;
for n=1:numel(A)
if strcmpi(A{n},'[data]'),n_start=n+1;end
if n_start>0 && n>=n_start && ~any(A{n}(1)=='-0123456789')
n_end=n-1;break
end
end
B=A(n_start:n_end);
for n=1:numel(B)
s=strfind(B{n},',');
split=zeros(1,numel(s)+1);
start_index=[s numel(B{n})+1];
stop_index=[0 s];
for m=1:numel(start_index)
str=B{n}((stop_index(m)+1):(start_index(m)-1));
split(m)=str2double(str);
end
B{n}=split;
end
B=cell2mat(B);
else
B = readmatrix('IN01_SCALED.txt');
nonan = ~isnan(B);
B = [B(nonan(:,1),1) B(nonan(:,2),2)];
end
0 Comments
Sign in to comment.