Got Questions? Get Answers.
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:
strange behavior of clc + fprintf() in a loop

Subject: strange behavior of clc + fprintf() in a loop

From: Yuval

Date: 21 Dec, 2011 22:37:07

Message: 1 of 11

can anyone explain this strange phenomenon: when running in a loop, calling (clc, fprintf, drawnow) will sometimes cause the Command Window to flash, even though nothing is happening between those commands.

Working example:

% try this code with load = {1,10,100,1000}
% why does the command window start flashing at some value?

str = repmat('some random string to print to the Command Window\n',[1 5]);

load = 1; % proxy for CPU load between iterations

for i = 1:1000
   % do useless stuff
   for j = 1:load
      m = randn(10);
   end
   
   % clear the command window and print the string
   clc
   fprintf(str);
   drawnow;
end

Subject: strange behavior of clc + fprintf() in a loop

From: Matt J

Date: 21 Dec, 2011 22:56:08

Message: 2 of 11

"Yuval" wrote in message <jctn2j$kv2$1@newscl01ah.mathworks.com>...
> can anyone explain this strange phenomenon: when running in a loop, calling (clc, fprintf, drawnow) will sometimes cause the Command Window to flash, even though nothing is happening between those commands.
====================

I don't see anything strange, unless the "flash" that you're talking about is the flickering of the printed text. But that makes sense to me. You're clearing the screen and reprinting the lines of text in a loop. So the text is repeatedly disappearing and reappearing at time intervals determined by "load".

Subject: strange behavior of clc + fprintf() in a loop

From: Yuval

Date: 21 Dec, 2011 23:08:08

Message: 3 of 11

> I don't see anything strange, unless the "flash" that you're talking about is the flickering of the printed text.

Yes, that's what I mean. This flickering makes it very hard to read. As you can imagine in the actual code the text is meaningful output that can change.

> But that makes sense to me. You're clearing the screen and reprinting the lines of text in a loop. So the text is repeatedly disappearing and reappearing at time intervals determined by "load".

No, it does not make sense. I'm clearing the screen and reprinting text in a loop, yes, but the "CPU load" code does not come between the clc and the drawnow. Why should I see a blank screen? Why should there be a flicker? (How can I get rid of it?)

Subject: strange behavior of clc + fprintf() in a loop

From: Matt J

Date: 21 Dec, 2011 23:29:08

Message: 4 of 11

"Yuval" wrote in message <jctoso$qap$1@newscl01ah.mathworks.com>...
>
>
> No, it does not make sense. I'm clearing the screen and reprinting text in a loop, yes, but the "CPU load" code does not come between the clc and the drawnow. Why should I see a blank screen? Why should there be a flicker? (How can I get rid of it?)
===========

Hmmm. Well, first of all I'm surprised that DRAWNOW would help here, since there's nothing in the documentation about it flushing an FPRINTF operation. It only seems to pertain to graphics updates, so you're kind of relying on an undocumented feature.

If I had to guess, the problem is that, even if drawnow does force the clc+fprintf to occur at a certain point, it cannot control how long it takes for each of these (that may depend on the state of the CPU and other background processes). So, in some cases, it may execute slow enough for the eye to follow... Dunno.

Subject: strange behavior of clc + fprintf() in a loop

From: Matt J

Date: 22 Dec, 2011 14:34:08

Message: 5 of 11

"Yuval" wrote in message <jctoso$qap$1@newscl01ah.mathworks.com>...
>
>
> Why should there be a flicker? (How can I get rid of it?)
===================


Do you really need to repeatedly execute clc+fprintf even when the text is not changing? Why not enclose these statements in an IF...END and reprint the text only when it changes. The following modification of your code is a rough imitation of this.


str = repmat('some random string to print to the Command Window\n',[1 5]);
   clc;
   fprintf(str);
   drawnow update


load = 1000; % proxy for CPU load between iterations

for i = 1:1000
   % do useless stuff
   for j = 1:load
      m = randn(10);
   end
   
   % clear the command window and print the string
   
  NewText= mod(i,100)==0;
   
  if NewText
      
   clc;
   
   fprintf(str);
   drawnow update
  end
   
end

Subject: strange behavior of clc + fprintf() in a loop

From: Yuval

Date: 22 Dec, 2011 19:02:09

Message: 6 of 11

> > Why should there be a flicker? (How can I get rid of it?)
> ===================
>
>
> Do you really need to repeatedly execute clc+fprintf even when the text is not changing?

Thanks for the effort Matt, but no cigar. As I mentioned above, in my real code the text is changing in every iteration (~10-40 milliseconds). It's giving me precise timing info about the actual computations that are being performed, but is nearly illegible because of the stupid flicker.

Perhaps there is some Java-based alternative to clc ?

Something that starts with the lines

mde = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = mde.getClient('Command Window');
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);

Subject: strange behavior of clc + fprintf() in a loop

From: Matt J

Date: 22 Dec, 2011 19:23:09

Message: 7 of 11

"Yuval" wrote in message <jcvurh$rdp$1@newscl01ah.mathworks.com>...
>
>
> Thanks for the effort Matt, but no cigar. As I mentioned above, in my real code the text is changing in every iteration (~10-40 milliseconds). It's giving me precise timing info about the actual computations that are being performed, but is nearly illegible because of the stupid flicker.
===================

If the text changes in every iteration, I imagine you'd need to slow the iterations down enough for the user's eye to follow the changes. Why not use pause(.1) or so, in place of drawnow?


 
> Perhaps there is some Java-based alternative to clc ?
======

Can't help you there. I have no background in Java.

Subject: strange behavior of clc + fprintf() in a loop

From: Ken Prager

Date: 23 Dec, 2011 13:11:01

Message: 8 of 11

In article <jctn2j$kv2$1@newscl01ah.mathworks.com>,
 "Yuval" <yuval.tassa@geemail.dot.com> wrote:

> can anyone explain this strange phenomenon: when running in a loop, calling
> (clc, fprintf, drawnow) will sometimes cause the Command Window to flash,
> even though nothing is happening between those commands.
>
> Working example:
>
> % try this code with load = {1,10,100,1000}
> % why does the command window start flashing at some value?
>
> str = repmat('some random string to print to the Command Window\n',[1 5]);
>
> load = 1; % proxy for CPU load between iterations
>
> for i = 1:1000
> % do useless stuff
> for j = 1:load
> m = randn(10);
> end
>
> % clear the command window and print the string
> clc
> fprintf(str);
> drawnow;
> end

Try using a terminal window instead.

That is, type "matlab -nodesktop"

KP

Subject: strange behavior of clc + fprintf() in a loop

From: Yair Altman

Date: 24 Dec, 2011 22:24:08

Message: 9 of 11

Instead of clearing the Command Window, erase just the last message, replacing it with a modified string, as follows:

reverseStr = '';
for idx = 1 : someLargeNumber
   % do computation here...
   
   fprintf([reverseStr, msgWithoutNewline]);
   reverseStr = repmat(sprintf('\b'), 1, length(msgWithoutNewline));
end


Yair Altman
http://UndocumentedMatlab.com

Subject: strange behavior of clc + fprintf() in a loop

From: Yuval

Date: 26 Dec, 2011 16:52:08

Message: 10 of 11

Yes.
Perfect.
Using the backspace character sprintf('\b') worked like a charm.
Thanks Yair !

"Yair Altman" wrote in message <jd5je8$1mk$1@newscl01ah.mathworks.com>...
> Instead of clearing the Command Window, erase just the last message, replacing it with a modified string, as follows:
>
> reverseStr = '';
> for idx = 1 : someLargeNumber
> % do computation here...
>
> fprintf([reverseStr, msgWithoutNewline]);
> reverseStr = repmat(sprintf('\b'), 1, length(msgWithoutNewline));
> end
>
>
> Yair Altman
> http://UndocumentedMatlab.com

Subject: strange behavior of clc + fprintf() in a loop

From: Steven_Lord

Date: 31 Dec, 2011 05:09:33

Message: 11 of 11



"Yuval" <yuval.tassa@geemail.dot.com> wrote in message
news:jcvurh$rdp$1@newscl01ah.mathworks.com...
>> > Why should there be a flicker? (How can I get rid of it?)
>> ===================
>>
>>
>> Do you really need to repeatedly execute clc+fprintf even when the text
>> is not changing?
>
> Thanks for the effort Matt, but no cigar. As I mentioned above, in my real
> code the text is changing in every iteration (~10-40 milliseconds). It's
> giving me precise timing info about the actual computations that are being
> performed, but is nearly illegible because of the stupid flicker.
>
> Perhaps there is some Java-based alternative to clc ?

Do you really need to read the timing information in (roughly) real time?
I'm guessing the answer is no, in which case writing the information to a
log file (for later viewing) would be preferable to displaying it on the
screen.

Alternately, display the information in a dialog box (like MSGBOX) and
simply change the String property of the control containing the text each
time it updates. That way you won't be trying to clear and rewrite to the
Command Window each time.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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