有没有一种方法可以用Matlab在计时器功能中减去/求和经过的时间? [英] Is there a way to subtract/sum the elapsed time in Timer function with Matlab?

查看:160
本文介绍了有没有一种方法可以用Matlab在计时器功能中减去/求和经过的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个回调计时器函数call_time(obj,event).我想知道计时器功能启动后执行过程中的经过时间(delt_time). 此外,我想使用该经过的时间来确定该函数将继续执行还是终止(例如delt_time> 60s).我希望计时器功能可以同时确定运行时间.通过这种方式,一旦程序达到阈值,代码便知道何时终止程序.实际上,根据我尝试过的不同方式,我问了几个类似的问题.但是还没有答案.

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.

现在我已经尝试过

     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

推荐答案

假设您要跟踪自回调进入以来的时间,可以使用tic/toc:

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 -Matlab执行是单线程的,因此开箱即用.您可以生成Java脚步并让另一个脚步终止,但是显然您将无法在其中运行Matlab代码(至少不容易,至少如此).

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).

对于纯Java解决方案,您可以查看问题.如果确实需要终止Matlab代码,则可以使用上述Java解决方案,并通过 MATLAB Java引擎API (实际上,我不确定在这种情况下该线程是否会终止).即使它确实起作用,也比简单地在语句之间添加一堆toc检查要复杂得多.

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.

这篇关于有没有一种方法可以用Matlab在计时器功能中减去/求和经过的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆