Path: news.mathworks.com!not-for-mail
From: "Zahra" <zahra.yamani@nrc.gc.ca>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to read and write a portion of a file
Date: Mon, 17 Nov 2008 23:18:02 +0000 (UTC)
Organization: National Research Council of Canada
Lines: 34
Message-ID: <gfsu3a$slj$1@fred.mathworks.com>
References: <gfcj8m$ksi$1@fred.mathworks.com> <gfo3f5$a94$1@fred.mathworks.com> <gfpq4i$mte$1@fred.mathworks.com>
Reply-To: "Zahra" <zahra.yamani@nrc.gc.ca>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226963882 29363 172.30.248.37 (17 Nov 2008 23:18:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 17 Nov 2008 23:18:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1060562
Xref: news.mathworks.com comp.soft-sys.matlab:501310


Hi Andres,

Thanks for your suggestion. I now have written the follwoing code (with the help of readline.m from file exchange) and for the same data file the time that it takes is almost 1/10 of my original code with a for loop. The first part of the code is to determine the line number for the interested runs and the last part uses the readline to read this portion of data file and then I used dlmwrite to write what was read exactly with the same format. Would be great if still posiible to make it faster. Any suggestion is greatly appreciated. Thanks. Zahra

filename= input('Enter the name of data file:');
scan1= input('Enter the initial run number:');                  
scan2= input('Enter the final run number:');
scan2=scan2+1;
string1='Run';
RunS1=sprintf('%6d',scan1);
searchS1=strcat(string1,RunS1);
RunS2=sprintf('%6d',scan2);
searchS2=strcat(string1,RunS2);
fid=fopen(filename,'r');
totalstring=textscan(fid,'%s','delimiter','\n');
stringchar=cellstr(totalstring{1});
totalnLines=length(stringchar);
for i=1:length(stringchar)
   if length(strfind(stringchar{i},searchS1)) ~= 0
   foundrun=1;
   Lscan1=i;
   end
   if length(strfind(stringchar{i},searchS2)) ~= 0
   foundrun=1;
   Lscan2=i;
   end
end
Lines=[Lscan1:Lscan2-1];
All=readline(filename,Lines,1);
dlmwrite('temp.dat', All, '')