Hello everyone. I have attached a .txt file (portion.txt) which contains a portion of my data. What I need is to create a script which will identify strings that correspond to pairs of x-y coordinates and return their line numbers. For instance, in the .txt file the first set of coordinates begins at line 3 and ends at line 138 (the number of those pairs is written above each set of coordinates, which at this case is 136). So the script should return those two numbers. Then this process should be done for the whole file. I suppose that the process can be repeated with loop since every next set of coordinates begins after 2 lines from the previous one. How can this be done? Thanks in advance.
MATLAB: How to extract line numbers of text data
extract line numbersline numbersread line numbers
Related Solutions
You are closing the file in the loop. close the file after the loop. I assume the below should work.
function read_datapattern(filename)fid = fopen(filename,'rt');if fid < 0 error('error opening file %s\n\n',filename);endpattern = fgets(fid);for n = 1:32767 nextline = fgets(fid); pattern = strcat(pattern, nextline);fprintf(pattern);fprintf('\n');endfclose(fid);
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
endfunction [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
endfunction [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 endfunction [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 recordendfunction [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)).';endfunction 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 endend
Best Answer