0

Say we have a call-back timer function call_time(obj, event). I want to know the elapsed time (delt_time) during the execution of timer function once it is started. Furthermore, I want to use that elapsed time to decide if the function will be continued executing or be terminated (say delt_time > 60s). I want the timer function to determine the running time concurrently. By doing this way, the code knows when to terminate the program once it reaches to the threshold. Actually, I have asked a couple of similar questions on the basis of different ways that I have tried. But no answers yet.

Now I've tried

     function call_time(obj, event)
         event_time = event.Data.time;
         event_time = event.Data.time - event_time;
         while event_time < 60
             %execute commands
         end
         if event_time > 60
             %terminate execution
         end
     end

But it does not work.Below is how I call the timer function.

     TimerHandle = timer;
     TimerHandle.StartFcn = @(~,thisEvent)disp([thisEvent.Type ' executed '...
         datestr(thisEvent.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
     TimerHandle.TimerFcn = @call_time;
     TimerHandle.StopFcn = @TimerCleanup;
     TimerHandle.period = 10;
     TimerHandle.ExecutionMode = 'fixedRate';
     start(TimerHandle);

I also tried the way that Tom suggested. But not working as well.

     function call_time(obj, event)
         event_time = event.Data.time;
         delta_time = event.Data.time - event_time;
         while delta_time < 60
             %execute commands
             delta_time = event.Data.time - event_time;
             fprintf('Elapsed %.2f sec\n', delta_time);
         end
         if delta_time > 60
             %terminate execution
         end
     end
Orangeblue
  • 229
  • 1
  • 5
  • 15

1 Answers1

0

Assuming you want to track time since the callback entrance, you can use tic/toc:

function call_time(obj, event)
    elapsed_sec = 0;
    t = tic();
    while elapsed_sec < 60
        % execute commands, e.g. something time-consuming
        A = randn(10000);
        elapsed_sec = toc(t);
        fprintf('Elapsed %.2f sec\n', elapsed_sec);
    end
end

UPDATE on concurrency - Matlab execution is single-threaded, so nothing like this exists out of the box. You could spawn java treads and have one terminate another, but you would obviously not be able to run Matlab code inside (not easily, at least).

For a pure java solution, you can check out this question. If you really-really need to terminate Matlab code, you can use aforementioned Java solution, and call back from Java to Matlab via JMI / MatlabControl or MATLAB Engine API for Java (I am actually not even certain the thread would be terminated in this case). Even if it does work, this is unnecessarily more complicated than simply adding a bunch of toc checks between your statements.

Community
  • 1
  • 1
nirvana-msu
  • 3,877
  • 2
  • 19
  • 28
  • tic/toc does not do it concurrently with the execution of "% execute commands, e.g. something time-consuming" part. Is there an alternative? – Orangeblue May 16 '16 at 22:47
  • You did not mention that you want it to be concurrent - please update your question. Matlab is single-threaded, so nothing like this exists out of the box. You could spawn java treads and have one terminate another, but you would obviously not be able to run Matlab code inside. For a pure java solution, you can check out [this question](http://stackoverflow.com/questions/2733356/killing-thread-after-some-specified-time-limit-in-java). – nirvana-msu May 16 '16 at 22:51
  • Okay I will update it. That's also the reason I did not use tic/toc, instead, using timer. – Orangeblue May 16 '16 at 22:54
  • Timer doesn't change anything - execution still happens in a single thread. – nirvana-msu May 16 '16 at 22:55
  • My idea was, knowing the execution time concurrently, and use that elapsed time to give the order whether terminate the program or not. – Orangeblue May 16 '16 at 22:57
  • Again, you cannot just 'give order'. The only possible way you can do that in Matlab is by checking elapsed time between your calculations (unless you decide to use Java + calling back to Matlab via JMI, which is a huge overkill) – nirvana-msu May 16 '16 at 23:00
  • The "% execute commands" is a huge collection of model study&solution settings. Not familiar with JML. I will look into that first. – Orangeblue May 16 '16 at 23:04
  • Why does it matter if the code is large or not? You can just `break`/`return` from any point within your code after checking elapsed time. – nirvana-msu May 16 '16 at 23:09
  • Cant do that. Because we do not know the exact code/line cause the infinite execution time. Even if knows, we will only be able to run the break/return commands after the execution, which is infinity. – Orangeblue May 16 '16 at 23:12