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

Optimizing the 'drawnow' function for an oscillating circle

Asked by Alan Armstrong on 24 May 2012

Hello,

I am using Matlab 7.10.0 to create an oscillating circle that moves horizontally across the screen. However the quality of the circle is quite poor probably due to the use of the 'drawnow' function being used to re-draw the circle as quickly as possible for every new position.

Is there any way that I would be able to optimize the draw function so that I can have a clean visual of the oscillating circle. Also would a computer with a higher processing speed and improved graphics card as well as a screen with a higher refresh rate improve the quality of the visual?

Here is a sample of the code that I am using. The code is set up as a function where I can call the specific type of visual that I want in this case it is a oscillation:

% --OSCILLATION SETUP

f = 0.5; % oscillation frequency Hz

A = 2.5; % oscillation amplitude

tmax = 10; % max time

t = 0:0.0001:tmax; % time array

x = A*sin(2*pi*f*t); % circle position array

% --VISUAL SETUP r = 1; % radius N = 100; % number of points on the cirle circle_colour = [0 0 0]; % circle colour RGB

 % play visual
if strcmp(visual_options,'oscillate')
    % plot moving circle
    while toc < tmax
        current_time = toc;
        ind = find(t>toc, 1, 'first');
        circle([x(ind) 0],r,N,circle_colour);
        axis([-xlim xlim -ylim ylim])
        axis off
        drawnow()
    end

Many thanks in advance,

Alan

0 Comments

Alan Armstrong

Products

No products are associated with this question.

4 Answers

Answer by Daniel on 24 May 2012
Accepted answer

To get the graphics really good you need to access the graphics card directly. Toolboxes like MGL and PsychToolbox help you do that.

2 Comments

Alan Armstrong on 25 May 2012

Thanks! I've decided to use PsychToolbox for creating my visuals it's a much easier program to work with and the quality of the visuals are much better than what I can achieve using figures.

Daniel on 25 May 2012

If you only want the visual part, check out MGL.

Daniel
Answer by owr on 24 May 2012

What are you doing in your function "circle"?

If you are calling a function like "plot" over and over again, that is creating alot more overhead than you need. You should be able to call it once, grab a handle to the graphics object and just update the 'xdata' and 'ydata' properties. You shouldnt need to call "drawnow" or "axis" within the loop.

Something like this:

theta = 0:pi/20:2*pi;
x = cos(theta);
y = sin(theta);
h = plot(x,y);
axis([-1,7,-4,4]);
for i = 1:100
    x = x + 0.05;
    set(h,'xdata',x);
    pause(0.1);
end

I used a for-loop and a pause state,emt, but agree with the other answers that suggested using a timer would be better.

1 Comment

Jan Simon on 25 May 2012

+1:Exatcly. While PAUSE(0.1) might be useful or not, using set(h, 'xdata') is a good strategy as well as avoiding the repeated time-consuming AXIS command. A fast iteration requires to avoid all unnecessary function calls.

owr
Answer by Sean de Wolski on 24 May 2012

No comment on the drawnow but I will say, using a timer object instead of a while-loop should really help stabilize your code.

doc timer

1 Comment

Alan Armstrong on 24 May 2012

Thank you Sean I'll substitute in the timer for the while-loop and see if it helps.

Thanks again,

Alan

Sean de Wolski
Answer by Stephen on 24 May 2012

throw a pause(0.02) in the while loop after the drawnow

3 Comments

Sean de Wolski on 24 May 2012

NO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Don't do this!!!!!!!! (I wish I could mark this in bold red size 72 font) Use TIMERs, that's what they were invented for!

Jan Simon on 25 May 2012

Why, Sean? PAUSE(0.02) flushs accumulated Java events for reasons I don't know. I do not think that this is very helpful here, but I do not think that a warning demands for so many exclamation marks.

Daniel on 25 May 2012

I don't know why a timer was invented for a "single threaded" environment like MATLAB, but I am pretty sure it wasn't for working on the 20 ms time scale. As for PAUSE, if I want to flush the event queue and risk leaving my circle update function for an indeterminate amount of time, then I will flush the queue myself with DRAWNOW thank you very much. I would use a resource hogging loop that keeps checking toc (or etime) to get the best timing. Until MATLAB becomes truly multi-threaded, it should stay single threaded.

Stephen

Contact us