0

I have a function func1 that calls another function func2, which, in turn, starts a timer object. I need func1 to pause until the timer stops. How can I do this?

The timer is not a SingleShot Timer but a Periodic Timer stopped using stop() on a condition tested in its TimerFcn.

3
  • 2
    The timer doesn not stop when it calls @timerCallback but only after it finished calling the callback (as many times as you define, but y default just once). Commented Aug 21, 2013 at 14:34
  • Yes, you are right, the timer stops when it finished calling the callback, but still I have the problem Commented Aug 21, 2013 at 14:54
  • Please, provide a sscce.org Commented Aug 21, 2013 at 15:34

2 Answers 2

3

Use wait() to halt execution until the timer stops:

function func1
    thandle = func2;
    wait(thandle)
    disp(1)
end

function h = func2
    h = timer('StartFcn',@(ev,obj)fprintf('Started timer'),'TimerFcn',@(ev,obj)pause(2));
    start(h)
end

Note that func2() is a subfunction of func1() but can also be an external function, the only requirement is that it returns the handle to the timer object.

Try the example by simply calling foo in the command window.

Sign up to request clarification or add additional context in comments.

5 Comments

This solution does not work because my timer has an infinite number of tasks to execute and it is stopped using "stop()". I get this error: "Can't wait with a timer that has an infinite TasksToExecute." function th = subfoo global h; %h = timer('StartFcn',@(ev,obj)fprintf('Started timer'),'TimerFcn',@(ev,obj)pause(5)); th = timer('Name', 'Refresher', 'TimerFcn',@refresherCallback,'ExecutionMode','fixedRate','Period', 0.5); h = th; start(th) end function refresherCallback(~,~) global h; counter = get(h, 'TasksExecuted'); if ( counter > 10) stop(h); end end
@Bedo Elaborate your orginal question to give more context to what you are trying to accomplish. Otherwise it's impossible to provide a working solution to a generic question.
Yes, sorry. The original question contains all detail needed. I have just added the fact that the timer is not a SingleShot but a Periodic timer stopped on a condition tested in its TimerFcn.
At the end I decided to start from this solution and to add a new SingleShot timer with a very high start delay that stops when my periodic timer stops. It works! Thank you
What I was asking to give you better directions is the general workflow that you want oto achieve. I suspect that what you're trying to accomplish can be done in a simpler way. Feel free to update your question or ask another one if you encounter problems with the complexity of the overall structure
2

You can return the timer object to func1 and implement a wait loop there:

while strcmp(to.Running, 'on')
    % Do Nothing
end

4 Comments

Will this work if func1 and func2 are on two different .m files? And is this a busy waiting? Busy waiting is not acceptable for me...
No it will wait until the timer has stopped running, either as it is being stopped or it has finished its execution command.
I tried and it works but the while cycle is a busy waiting and Matlab uses 100% of my CPU for nothing (and for a lot of time). This is not a good idea :(
I added a pause(2) inside the while and now it works better since I don't need real time execution. Still I think this is a work around...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.