Optimizing the 'drawnow' function for an oscillating circle

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

 채택된 답변

Daniel Shub
Daniel Shub 2012년 5월 24일

0 개 추천

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

댓글 수: 2

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.
If you only want the visual part, check out MGL.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

owr
owr 2012년 5월 24일
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

+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.

댓글을 달려면 로그인하십시오.

Sean de Wolski
Sean de Wolski 2012년 5월 24일
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

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

댓글을 달려면 로그인하십시오.

Stephen
Stephen 2012년 5월 24일

0 개 추천

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

댓글 수: 3

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!
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.
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by