How to create a timer object to replace pause function?

조회 수: 4 (최근 30일)
Andrew P
Andrew P 2014년 3월 31일
댓글: Joseph Cheng 2014년 4월 1일
I want to create a timer function that will replace the pause function. I have a GUI that is controlling the operation of another program. I send commands to the other program, then need to wait a few seconds before sending the next command, then need to wait a few seconds in between polling the status. I'm currently using pause(5), but would like to use a timer instead so that I can still use the command window while the GUI is waiting. I couldn't find an example of how to set up a singleshot timer that replaces the pause function.
Here's an example of what my code looks like (with pseudocode for the functions that interact with the other program):
function CtrlOtherProgram
sendcmd('start simulation') % sendcmd is representative of subfcn that controls other program
pause(10) % give it ten seconds. Sending next cmd immediately causes errors
while strcmp('running',sendcmd('check status')) % while simulation is still running
pause(5) % give a few seconds between polling, to avoid overwhelming other program
end
sendcmd('stop simulation')
pause(15)
end
How can I write a timer function/object that will replace pause(pausedur)? Unless I'm just misunderstanding timers, this seems like an example that should be included in the MATLAB help documentation.
Thanks!

답변 (1개)

Joseph Cheng
Joseph Cheng 2014년 3월 31일
편집: Joseph Cheng 2014년 3월 31일
see my example for using/starting timer. In the linked question the question was how to change the colour of the button in a defined time. http://www.mathworks.com/matlabcentral/answers/123271-matlab-guide-change-color-while-loop-is-running
here you would need to swap out the timer function call back with some scheme to achieve the the desired timing. Suggestions flag variables which you can check the state of things the next time timer polls the call back function? if command sent and it has been 5 secs perform X.
  댓글 수: 2
Andrew P
Andrew P 2014년 4월 1일
Using the flag variable idea, it appears that I have to use either wait or pause command in a while loop
Here's how i tried implementing it:
function testingtimers
delete(findobj('Name','TimerTesting'));
handles.figure=figure('Name','TimerTesting'); %using figure 'UserData' to store variables
tic
handles.pausedur=5;
handles.timerflag = 0;
handles.pausetimer = 0; timer('ExecutionMode','fixedRate','Period',1,'TimerFcn',{@PauseTimerCallback});
set(handles.figure,'UserData',handles);
start(handles.pausetimer)
while ~handles.timerflag % check timer flag
handles=get(handles.figure,'UserData');
end
stop(handles.pausetimer)
delete(handles.pausetimer)
pausetime = toc
end
function PauseTimerCallback(~,~,~)
handles=get(gcf,'UserData');
elapsetime=toc;
if round(elapsetime)>= handles.pausedur
handles.timerflag = 1;
set(gcf,'UserData',handles) %set figure 'UserData' to pass data back to while loop
end
end %end PauseTimerCallback function
I can't think of how to wait for the timer callback to set a flag without using wait, pause, or continuously running through a while loop.
Do you know for sure whether it's actually possible to use a timer to give the same functionality as pause, but allowing other functions to execute (or me to use the command window) while it's waiting?
Thanks for your help!
Joseph Cheng
Joseph Cheng 2014년 4월 1일
Here is a very quick thing i created. it doesn't exit the GUI eloquently. As you can see if you type in something into the controller box and hit command you can see it cycle in one letter into the Receiver. It checks to see if the time elapse since last "command" has been greater than 5 seconds. You probably do not want to go with what i did with Global variables but as a short demonstrations I chose some shortcuts.

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by