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:
How to stop graphics refresh

Subject: How to stop graphics refresh

From: Aleks

Date: 2 Jul, 2008 19:34:01

Message: 1 of 9

Does anyone know if there is a way to stop refreshing
graphics in matlab? I have a quite complex graphics
program, and it looks like the figure sometimes updates
between calls to drawnow. It makes the image flicker.

What I want is something like:

while(running)
  do something to figure;
  do something else to figure;
  do even more to figure;
  drawnow;
end

I need to make sure that the figure is not updated unless
drawnow is called.

Thanks for your help!

Subject: How to stop graphics refresh

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 2 Jul, 2008 20:16:06

Message: 2 of 9

In article <g4gl79$e9t$1@fred.mathworks.com>,
Aleks <aleksve@hotmail.com> wrote:
>Does anyone know if there is a way to stop refreshing
>graphics in matlab? I have a quite complex graphics
>program, and it looks like the figure sometimes updates
>between calls to drawnow. It makes the image flicker.

Does the figure need to be visible during the update?
I guess if flickering is a problem then Yes, but if it doesn't
happen to need to be visible during the update, set the figure
visibility off, do the update, and then set the figure visibility
on.

If the figure does need to be visible, then you could do a double-figure
process: when you want to do the update, create an invisible figure,
do all the graphics writing to it, and when it is done, make the
new figure visible and the old figure invisible.

--
  "To burn always with this hard, gem-like flame, to maintain this
  ecstasy, is success in life." -- Walter Pater

Subject: How to stop graphics refresh

From: Aleks

Date: 11 Jul, 2008 11:31:02

Message: 3 of 9

If only it was that good... The figure always has to be
visible as it is part of an animation. I am becoming more
and more convinced that the error lies somewhere else. I
tested it with following one-liner:

i = 0:0.01:2*pi; h = plot(sin(i)); drawnow; while(true),
for j = 0:0.01:2*pi; set(h, 'YData', sin(i-j)); end; end

If the figure updated itself without explicit call to
drawnow, the line would produce a running sinus wave.
However, the figure updates itself only once, when the
first sinus is drawn. On the other hand,

i = 0:0.01:2*pi; h = plot(sin(i)); drawnow; while(true),
for j = 0:0.01:2*pi; set(h, 'YData', sin(i-j)); drawnow;
end; end

(added "drawnow" just before "end; end") successfully
produces a running sinus wave.

Well, thanks for your time anyway :=)

best regards, Aleksander



roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g4gnm6$cnk$1@canopus.cc.umanitoba.ca>...
> In article <g4gl79$e9t$1@fred.mathworks.com>,
> Aleks <aleksve@hotmail.com> wrote:
> >Does anyone know if there is a way to stop refreshing
> >graphics in matlab? I have a quite complex graphics
> >program, and it looks like the figure sometimes updates
> >between calls to drawnow. It makes the image flicker.
>
> Does the figure need to be visible during the update?
> I guess if flickering is a problem then Yes, but if it
doesn't
> happen to need to be visible during the update, set the
figure
> visibility off, do the update, and then set the figure
visibility
> on.
>
> If the figure does need to be visible, then you could do
a double-figure
> process: when you want to do the update, create an
invisible figure,
> do all the graphics writing to it, and when it is done,
make the
> new figure visible and the old figure invisible.
>
> --
> "To burn always with this hard, gem-like flame, to
maintain this
> ecstasy, is success in life." -- Walter
Pater

Subject: How to stop graphics refresh

From: Jos

Date: 11 Jul, 2008 11:43:02

Message: 4 of 9

"Aleks " <aleksve@hotmail.com> wrote in message
<g57g9m$ap5$1@fred.mathworks.com>...
> If only it was that good... The figure always has to be
> visible as it is part of an animation. I am becoming more
> and more convinced that the error lies somewhere else. I
> tested it with following one-liner:
>
> i = 0:0.01:2*pi; h = plot(sin(i)); drawnow; while(true),
> for j = 0:0.01:2*pi; set(h, 'YData', sin(i-j)); end; end
>
> If the figure updated itself without explicit call to
> drawnow, the line would produce a running sinus wave.
> However, the figure updates itself only once, when the
> first sinus is drawn. On the other hand,
>
> i = 0:0.01:2*pi; h = plot(sin(i)); drawnow; while(true),
> for j = 0:0.01:2*pi; set(h, 'YData', sin(i-j)); drawnow;
> end; end
>
> (added "drawnow" just before "end; end") successfully
> produces a running sinus wave.
>
> Well, thanks for your time anyway :=)
>
> best regards, Aleksander
>
>
>
> roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
> message <g4gnm6$cnk$1@canopus.cc.umanitoba.ca>...
> > In article <g4gl79$e9t$1@fred.mathworks.com>,
> > Aleks <aleksve@hotmail.com> wrote:
> > >Does anyone know if there is a way to stop refreshing
> > >graphics in matlab? I have a quite complex graphics
> > >program, and it looks like the figure sometimes updates
> > >between calls to drawnow. It makes the image flicker.
> >
> > Does the figure need to be visible during the update?
> > I guess if flickering is a problem then Yes, but if it
> doesn't
> > happen to need to be visible during the update, set the
> figure
> > visibility off, do the update, and then set the figure
> visibility
> > on.
> >
> > If the figure does need to be visible, then you could do
> a double-figure
> > process: when you want to do the update, create an
> invisible figure,
> > do all the graphics writing to it, and when it is done,
> make the
> > new figure visible and the old figure invisible.
> >
> > --
> > "To burn always with this hard, gem-like flame, to
> maintain this
> > ecstasy, is success in life." -- Walter
> Pater
>

Try to set the doublebuffer property to on.

Jos

Subject: How to stop graphics refresh

From: Nasser Abbasi

Date: 11 Jul, 2008 16:09:43

Message: 5 of 9


"Aleks " <aleksve@hotmail.com> wrote in message
news:g57g9m$ap5$1@fred.mathworks.com...

> i = 0:0.01:2*pi; h = plot(sin(i)); drawnow; while(true),
> for j = 0:0.01:2*pi; set(h, 'YData', sin(i-j)); end; end
>
> If the figure updated itself without explicit call to
> drawnow, the line would produce a running sinus wave.
> However, the figure updates itself only once, when the
> first sinus is drawn. On the other hand,
>
> i = 0:0.01:2*pi; h = plot(sin(i)); drawnow; while(true),
> for j = 0:0.01:2*pi; set(h, 'YData', sin(i-j)); drawnow;
> end; end
>
> (added "drawnow" just before "end; end") successfully
> produces a running sinus wave.
>

I've always put a drawnow when I want stuff to appear on display. So I am
not sure why you were expecting the plot to show up inside the loop without
having to add a drawnow there?

Just becuase you put a "drawnow" outside the loop has no effect.

This is what I normally do.

loop
   plot(...);
    .......
  plot(..)
   .......
  drawnow;
  pause(...)
end


Nasser

Subject: How to stop graphics refresh

From: Alon

Date: 9 Mar, 2009 07:23:02

Message: 6 of 9

I encountered this problem myself in several occasions, and didn’t find anywhere an elegant solution.
I event went into java in hope to control the event-queue.
Finally - giving up on an elegant solution - I approached the problem in "brought force", with the presented solution, which visually gave me exactly what I wanted.
Basically what I'm doing is a crude Z-Buffer:
I copy the figures content, display it in front, and after all the graphic operations have finished - delete the overlaying image:

F=getframe(figH);
D=F.cdata; Handle=uicontrol(figH,'Style','pushbutton','units','normalized','position',[0,0,1,1],'cdata',D);
uistack(Handle,'top');
drawnow;
%...
% All the graphic operatoins
%...
drawnow;
pause(0.1);
delete(Handle);

Hope that helps,
Alon Walter.

Subject: How to stop graphics refresh

From: Yair Altman

Date: 9 Mar, 2009 08:16:01

Message: 7 of 9

"Alon " <zarboov@hotmail.com> wrote in message
[snip]
> I copy the figures content, display it in front, and after all the graphic operations have finished - delete the overlaying image:
>
> F=getframe(figH); D=F.cdata;
> Handle=uicontrol(figH,'Style','pushbutton','units','normalized','position',[0,0,1,1],'cdata',D);
> uistack(Handle,'top');
> drawnow;
> %...
> drawnow;
> pause(0.1);
> delete(Handle);

This is a great trick, Alon :-)
The only drawbacks are that the main figure toolbar disappears during the operation. Also, the image does not resize when the figure resizes, so perhaps it's best to make the figure modal for the duration.
I would also suggest adding (...,'enable','inactive') so the uicontrol is not "clickable".

Yair Altman

Subject: How to stop graphics refresh

From: Rodney Thomson

Date: 9 Mar, 2009 08:38:01

Message: 8 of 9

"Aleks " <aleksve@hotmail.com> wrote in message <g4gl79$e9t$1@fred.mathworks.com>...
> Does anyone know if there is a way to stop refreshing
> graphics in matlab? I have a quite complex graphics
> program, and it looks like the figure sometimes updates
> between calls to drawnow. It makes the image flicker.
>
> What I want is something like:
>
> while(running)
> do something to figure;
> do something else to figure;
> do even more to figure;
> drawnow;
> end
>
> I need to make sure that the figure is not updated unless
> drawnow is called.
>
> Thanks for your help!

> I need to make sure that the figure is not updated unless
> drawnow is called.

I normally have the opposite problem where graphics are NOT redrawn when I wish them to. That is where a drawnow can be useful.

The default behavior for MATLAB is to not redraw unless one of the following occurs (taken from the doc page for 'drawnow')

-- Other Events That Cause Event Queue Processing --

Other events that cause MATLAB to flush the event queue and draw the figure includes:

    * Returning to the MATLAB prompt
    * Executing the following functions
          o figure
          o getframe
          o input
          o keyboard
          o pause
    * Functions that wait for user input (i.e., waitforbuttonpress, waitfor, ginput)
    * Any code that causes one of the above functions to be executed. For example, suppose h is the handle of an axes. Calling axes(h) causes its parent figure to be made the current figure and brought to the front of all displayed figures, which results in the event queue being flushed.

So if you don't want flickering, make sure none of your code in your loop does anything mentioned above and have a solitary drawnow at the end of each loop block. If so, then find an alternative method to acheive this (ie how you can do set(handle, 'XData', 0:10, 'YData', 10:-1:0) instead of plot(0:10, 10:-1:0))

Cheers

Rod

p.s. did noone else chuckle at the running sinus? Maybe its just me... Its been a long day here! Also sorry if I am double / triple posting. I'm getting a lot of server errors from Mathworks.

Subject: How to stop graphics refresh

From: Rune Allnor

Date: 9 Mar, 2009 10:02:36

Message: 9 of 9

On 9 Mar, 08:23, "Alon " <zarb...@hotmail.com> wrote:
> I encountered this problem myself in several occasions, and didn=92t find=
 anywhere an elegant solution.
> I event went into java in hope to control the event-queue.
> Finally - giving up on an elegant solution - I approached the problem in =
"brought force", with the presented solution, which visually gave me exactl=
y what I wanted.
> Basically what I'm doing is a crude Z-Buffer:

This ought to be formalized into matlab. What about
adding a figure (and/or axis) property 'redrawmode'
that could be set to 'auto' or 'manual'. In 'auto'
the figure behaves as it does today, with the figure
being redrawn on the fly. In 'manual' mode the graphics
is drawn in a buffer but the screen is not updated
until an explicit command (REDRAW ?) is called.

Something for TMW to consider?

Rune

Tags for 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