Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
writing to text file: Flush the buffer

Subject: writing to text file: Flush the buffer

From: Paul Skoczylas

Date: 29 Sep, 2005 19:26:57

Message: 1 of 3

I'm writing results of calculations to a text file.

Basically using something like:

fid=fopen('textfile.txt','wt');
while ~done;
   %bunch of calculations
   fprintf(fid,'%s, %g, %g',a,b,c); %or whatever
end;
fid=fclose(fid);

This works just fine, if I'm patient enough to wait until it's done before looking at the results.

But I'm not patient! I want to inspect the results while the program is running. No problem, I have a text editor which can read
the files while they're still open by Matlab.

The problem is that Matlab waits until a certain amount of text has accumulated before actually writing it to the file. If the
calculation goes quickly, that's no big deal, but for a slow one, it can be significant. (I have to let my code run for a few
minutes before I can see that it's doing what it's supposed to be.)

So the question is: Is there a way to force Matlab to flush the buffer after each fprintf statement?

I suppose I could close the file and reopen it for "append", but that seems rather inefficient... Any better ideas?

Thanks,

-Paul

Subject: writing to text file: Flush the buffer

From: Zeph Davies

Date: 30 Mar, 2011 13:46:04

Message: 2 of 3

"Paul Skoczylas" <pauls@cfertech.DELETE.com> wrote in message <5eX_e.317678$on1.69061@clgrps13>...
> I'm writing results of calculations to a text file.
>
> Basically using something like:
>
> fid=fopen('textfile.txt','wt');
> while ~done;
> %bunch of calculations
> fprintf(fid,'%s, %g, %g',a,b,c); %or whatever
> end;
> fid=fclose(fid);
>
> This works just fine, if I'm patient enough to wait until it's done before looking at the results.
>
> But I'm not patient! I want to inspect the results while the program is running. No problem, I have a text editor which can read
> the files while they're still open by Matlab.
>
> The problem is that Matlab waits until a certain amount of text has accumulated before actually writing it to the file. If the
> calculation goes quickly, that's no big deal, but for a slow one, it can be significant. (I have to let my code run for a few
> minutes before I can see that it's doing what it's supposed to be.)
>
> So the question is: Is there a way to force Matlab to flush the buffer after each fprintf statement?
>
> I suppose I could close the file and reopen it for "append", but that seems rather inefficient... Any better ideas?
>
> Thanks,
>
> -Paul
>
>


I would like to bump this very old post as it's a problem i currently have and this was not answered.

Zeph

Subject: writing to text file: Flush the buffer

From: Florin Neacsu

Date: 30 Mar, 2011 17:55:22

Message: 3 of 3

"Zeph Davies" wrote in message <imvc6s$avo$1@fred.mathworks.com>...
> "Paul Skoczylas" <pauls@cfertech.DELETE.com> wrote in message <5eX_e.317678$on1.69061@clgrps13>...
> > I'm writing results of calculations to a text file.
> >
> > Basically using something like:
> >
> > fid=fopen('textfile.txt','wt');
> > while ~done;
> > %bunch of calculations
> > fprintf(fid,'%s, %g, %g',a,b,c); %or whatever
> > end;
> > fid=fclose(fid);
> >
> > This works just fine, if I'm patient enough to wait until it's done before looking at the results.
> >
> > But I'm not patient! I want to inspect the results while the program is running. No problem, I have a text editor which can read
> > the files while they're still open by Matlab.
> >
> > The problem is that Matlab waits until a certain amount of text has accumulated before actually writing it to the file. If the
> > calculation goes quickly, that's no big deal, but for a slow one, it can be significant. (I have to let my code run for a few
> > minutes before I can see that it's doing what it's supposed to be.)
> >
> > So the question is: Is there a way to force Matlab to flush the buffer after each fprintf statement?
> >
> > I suppose I could close the file and reopen it for "append", but that seems rather inefficient... Any better ideas?
> >
> > Thanks,
> >
> > -Paul
> >
> >
>
>
> I would like to bump this very old post as it's a problem i currently have and this was not answered.
>
> Zeph


Hi,

Can you give a code reproducing this behavior?
This is what I tried and seems to update just fain (with a pause of just 5 sec)

fid=fopen('foo.txt','w');
a=1;
b=1;

for i=1:100
    fprintf(1,'new iteration i=%d \n',i);
    c=a+b;
    fprintf(fid,'%d\t%d\n',i+2,c);
    pause(5);
    a=b;
    b=c;
end
fclose(fid);

I am just guessing here, put try putting a "continue" at the end of your for loop. Might help somehow.

Regards,
Florin

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us