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,
No products are associated with this question.
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.
No comment on the drawnow but I will say, using a timer object instead of a while-loop should really help stabilize your code.