how can I move a Point in a plot (GUI) from one side to the other in a fixed time?

조회 수: 2 (최근 30일)
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?

채택된 답변

Robert Cumming
Robert Cumming 2014년 10월 19일
편집: Robert Cumming 2014년 10월 20일
edit: changed timing to an overall target for loop instead of interval time.
See the code below:
figure;
ax = axes;
x = [0:100];
h = plot ( ax, x, x*0 );
ax.YLim = [0 100];
targetTime = 3;
nSteps = 100;
intervalTime = targetTime./nSteps;
loop = tic;
for ii=1:nSteps
h.YData(:) = ii;
drawnow();
timeTaken = toc(loop);
% comment out this line to see the effet of the pause
pause(max(0,(intervalTime.*ii)-timeTaken));
end
toc(loop)
When I run this in matlab I get the following:
Elapsed time is 1.174296 seconds. (pause commented out)
Elapsed time is 3.006906 seconds.
  댓글 수: 2
Óscar
Óscar 2014년 10월 20일
Thanks, but I don't know why, but if I run it in my pc it the overall time is between 20.3 and 20.8, the first one it's OK but not the second one. Now if your code change eachLoopTarget=0.03, then the overall time should be 3seconds but it's at least 3.5... (and 0.5s more is too much for my app) Thank you anyway.
Robert Cumming
Robert Cumming 2014년 10월 20일
편집: Robert Cumming 2014년 10월 20일
There will be a small overhead of calculating the input to the pause function and for the loop itself.
You could test what that value is (around .5 from your figures above) and modify your eachLoopTarget variable accordingly.
edit I have changed my example code to target a total time rather than a explicit interval time.

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

추가 답변 (2개)

Image Analyst
Image Analyst 2014년 10월 18일
What happens to the time if you reduce the .02 to something less?
  댓글 수: 3
Image Analyst
Image Analyst 2014년 10월 18일
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
Óscar 2014년 10월 18일
Thank you, I don't really need it to be 3.000 seconds, it can be 2.9 or 3.1, but the things is that 3s it's only an example the user we'll have to select it, and he'll be able to choose whichever time he wants... now it's only for 1 point, but there are going to be up to ten points... so I'm just wondering which is the best choice to get a time as accurate as possible.
Thanks

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


Robert Cumming
Robert Cumming 2014년 10월 18일
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
Image Analyst
Image Analyst 2014년 10월 19일
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
Robert Cumming 2014년 10월 19일
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....)

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

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by