MATLAB: How to import specific part of text file when you do not known the size and which lines is the one you want to import

data importMATLABtext filetxt

The problem is I can't import specific data from text file. I tried fopens, but it shows me an error. Text file I try to use has more than 20000 lines, so it is impossible to do it manually. I have some data with headlines and it is divided into some parts. I need to use only one section, but it is not always in the same order. But it has always the same name.
An example is attached. I need to import 'Dose Interpolated' part.
I would appreciate some help
Regards,
Anna

Best Answer

OK, here's a basic function to read any section of the file returning the data in that section plus optionally the X,Y coordinate vectors to go with it...
function [data,x,y]=readAnna(fname,section)
% returns desired sectional data from file

% alternate returns of X,Y coordinates if desired

%

% [data,x,y]=readAnna(fname,'Dose Interpolated')

% will return the interpolated dose section

fid=fopen(fname, 'r');
[r,c]=getrowcol(fid); % go get the row, col sizes

[data,x,y]=readsection(fid,section,r,c); % get desired data section

fid=fclose(fid); % close the file

end
function [r,c]=getrowcol(fid)
while ~feof(fid) % find the Rows: line
l=fgetl(fid);
if ~isempty(strfind(l,'Rows:')),break,end
end
r=sscanf(l,'Rows: %d'); % read the value from string

c=fscanf(fid,'Cols: %d'); % get column numbers next record

end
function [d,x,y]=readsection(fid,section,r,c)
while ~feof(fid)
l=fgetl(fid);
if ~isempty(strfind(l,section)),break,end
end
fgetl(fid); % skip the header record

d=fscanf(fid,'%f',[(c+2),r]).'; % read data including y,rec

y=d(:,1); % y is first column

d=d(:,3:end); % save only data

l=fgetl(fid); % resynch to begin next record

x=cell2mat(textscan(fid,['%*s' repmat('%f',1,c)],'headerlines',1)).'; % read the x coords
end
This can be called in a routine to process whatever file(s) are chosen; data storage for multiple files would be handled at that level; one alternative would be 3D array.
ADDENDUM
Slightly cleaned-up version replacing multiple lookup loops with function...
function [data,x,y]=readAnna(fname,section)
% returns desired sectional data from file
% alternate returns of X,Y coordinates if desired
%
% [data,x,y]=readAnna(fname,'Dose Interpolated')
% will return the interpolated dose section
fid=fopen(fname, 'r'); % open requested file
[r,c]=getrowcol(fid); % go get the row, col sizes
[data,x,y]=readsection(fid,section,r,c); % get desired data section
fid=fclose(fid); % close the file
end
function [r,c]=getrowcol(fid)
l=locaterecord(fid,'Rows:'); % get to proper record
r=sscanf(l,'Rows: %d'); % read the value from string
c=fscanf(fid,'Cols: %d'); % get column numbers next record
end
function [d,x,y]=readsection(fid,section,r,c)
l=locaterecord(fid,section); % find beginning of section
fgetl(fid); % skip the header record
d=fscanf(fid,'%f',[(c+2),r]).'; % read data including y,rec
y=d(:,1); % y is first column
d=d(:,3:end); % save only data
l=fgetl(fid); % resynch to begin next record
x=cell2mat(textscan(fid,['%*s' repmat('%f',1,c)],'headerlines',1)).';
end
function l=locaterecord(fid,str)
% find record in open file containing str; return as character string
while ~feof(fid)
l=fgetl(fid);
if ~isempty(strfind(l,str)),break,end
end
end