how can I move a Point in a plot (GUI) from one side to the other in a fixed time?
Show older comments
I have a point in a plot and I want to move it "smoothly" to the other side of the plot within a fixed amount of time. In the example this time is 3s, and I refresh the plot "every 20ms" (20ms is what I want, but it's always more, between: 30 and 50ms). My code is:
interval=3000/20; %3000/20=150
a=0;
for i=1:1:interval
iTime=clock;
set(pointHandle,'Xdata',point(1).coordinatesNow(1)+(500/150),'Ydata',point(1).coordinatesNow(2)); %Only refresh Xaxis
point(1).coordinatesNow(1)=point(1).coordinatesNow(1)+(500/150);
pause(0.02); %pause of 20ms
a=a+etime(clock,iTime); %total elapsed time
end
disp(a); %show total time
So I'd like "a" to be 3s, but most of the times is between 7 and 10s, How could I get more accuracy?
Accepted Answer
More Answers (2)
Image Analyst
on 18 Oct 2014
0 votes
What happens to the time if you reduce the .02 to something less?
3 Comments
Óscar
on 18 Oct 2014
Image Analyst
on 18 Oct 2014
It's almost impossible to get super precise timing with a multi-tasking operating system. I don't know why your uses need it to be exactly 3.00000 seconds, but if they do, maybe elevating the priority of the MATLAB process to High, or even "Real time" might work, though using Real time is very risky since it won't pay attention to anything else in your computer (like clicking on other applications) until your app is done.
Alternatively, look in to the Real Time toolboxes - maybe there's something there that can help.
Óscar
on 18 Oct 2014
Robert Cumming
on 18 Oct 2014
0 votes
You need to work out how long it takes to update the plot, then adjust your pause command to suit at runtime so that the code runs at the speed you want it to.
7 Comments
Óscar
on 18 Oct 2014
Robert Cumming
on 18 Oct 2014
Its not that pause is taking too long, pause is also trigering a screen refresh.
If you replace pause with drawnow you will see what I mean. I'm assuming it will still be too slow.
Óscar
on 18 Oct 2014
Robert Cumming
on 18 Oct 2014
You measure the time it takes to update the plot. If that is less than your target time for each step then issue a pause command which will pause for the remainder.
Óscar
on 19 Oct 2014
Image Analyst
on 19 Oct 2014
Maybe try a small for loop where it just spins it's wheel for a short time
tic
for k = 1:3000
end
toc
Elapsed time is 0.000008 seconds.
Adjust the ending value to trim it to shorter or longer times.
Robert Cumming
on 19 Oct 2014
I dont know wwhy you think pause will take longer, this time it would have no plot refresh so it should pause for the requested time.
See my other answer for an example code (I didn't edit this one as I answered it with an old account that I no longer use....)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!