Optimizing the 'drawnow' function for an oscillating circle

15 views (last 30 days)
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

Accepted Answer

Daniel Shub
Daniel Shub on 24 May 2012
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

Sign in to comment.

More Answers (3)

owr
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
Jan 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.

Sign in to comment.


Sean de Wolski
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
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

Sign in to comment.


Stephen
Stephen on 24 May 2012
throw a pause(0.02) in the while loop after the drawnow
  3 Comments
Daniel Shub
Daniel Shub 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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by