MATLAB Answers

How to create a timer object to replace pause function?

4 views (last 30 days)
Andrew P
Andrew P on 31 Mar 2014
Commented: Joseph Cheng on 1 Apr 2014
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!

Answers (1)

Joseph Cheng
Joseph Cheng on 31 Mar 2014
Edited: Joseph Cheng on 31 Mar 2014
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 Comments
Joseph Cheng
Joseph Cheng on 1 Apr 2014
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.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!