Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!z28g2000vbl.googlegroups.com!not-for-mail
From: TideMan <mulgor@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Textread
Date: Wed, 1 Jul 2009 16:53:38 -0700 (PDT)
Organization: http://groups.google.com
Lines: 66
Message-ID: <3f7d1327-8613-45d3-b288-e07e20bbe0be@z28g2000vbl.googlegroups.com>
References: <h2favd$6b6$1@fred.mathworks.com> <op.uwd76lf8a5ziv5@uthamaa.dhcp.mathworks.com> 
	<b6ceba46-a033-4fb4-a70f-967c776bf8d7@h8g2000yqm.googlegroups.com> 
	<h2gcut$ktu$1@fred.mathworks.com> <h2gnve$d8t$1@fred.mathworks.com>
NNTP-Posting-Host: 202.78.152.105
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1246492418 32643 127.0.0.1 (1 Jul 2009 23:53:38 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 1 Jul 2009 23:53:38 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: z28g2000vbl.googlegroups.com; posting-host=202.78.152.105; 
	posting-account=qPexFwkAAABOl8VUndE6Jm-9Z5z_fSpR
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11) 
	Gecko/2009060215 Firefox/3.0.11,gzip(gfe),gzip(gfe)
X-HTTP-Via: 1.1 bc1
Xref: news.mathworks.com comp.soft-sys.matlab:552240


On Jul 2, 10:29 am, "Jan Simon" <matlab.THIS_Y...@nMINUSsimon.de>
wrote:
> Dear Jesper Lauridsen!
>
> > f1id = fopen('data_in.dat');
> > f2id = fopen('repairedfile.dat','w');
>
> > while (~feof(f1id))
> >    s = getl(f1id);
> >    sidx = find(s==',');
> >    s(sidx) = '.';
> >    fprintf(f2id,'%s\n',s);
> > end
>
> > fclose(f1id);
> > fclose(f2id);
>
> Just some ideas:
>   sidx=find(s==',');
>   s(sidx) = '.';
> can be written faster as:
>   s(findstr(',')) = '.';
> This is usually even faster than:
>   s(s==',') = '.'
>
> Nevertheless, FGETL and FPRINTF have a remarkable overhead. FGETS and FWRITE can be remarkably faster:
>   fid1 = fopen('data_in.dat'); fid2 = fopen('repairedfile.dat','w');
>   while 1
>     s = fgets(fid1);
>     if ischar(s) == 0, break; end  % replace FEOF
>     fwrite(fid2, strrep(s, ',', '.'), 'uchar');
>   end
>   fclose(fid1); fclose(fid2);
>
> But if we are on the way, we can drop the WHILE loop and replace the original file immediately:
>   fid = fopen('data_in.dat', 'rb+');
>   s = fread(fid, inf, 'uchar');
>   fseek(fid, 0, -1);
>   fwrite(fid, strrep(s, ',', '.'), 'uchar');
>   fclose(fid);
> The 'b' mode of FOPEN is needed to keep the original line breaks.
>
> Here a surprising problem can appear in FSEEK: For effective multithreading, the operating system can stop FSEEK before it reaches the desired location. This happens more likely on heavy system load and slow (network-) drives, but it is really rare and never reproducible. (NOTE: This is not a Matlab problem.)
> Therefore it is safer to check the reply of FSEEK (-1 on failure) or FCLOSE and FOPEN the file again.
>
> Good luck, Jan

Even less complicated:

f1id = fopen('data_in.dat');
f2id = fopen('repairedfile.dat','w');

a=fscanf(f1id,'%c');  % Read everything into string
a=strrep(a,',','.');    % replace commas
fprintf(f2id,'%c',a);   % write everything out

fclose(f1id);
fclose(f2id)'