Time function. Execute a script every X seconds

2 vues (au cours des 30 derniers jours)
G
G le 11 Juin 2016
Commenté : G le 17 Juin 2016
I am trying to execute the following 3 lines of script every minute. For this, i am using the timer function but it seems that the 3 lines that i need are not executed properly.
refresh1.m (the 3 lines that i want to execute every X seconds)
fromdate = now-1;
todate = now;
timeseries(c,sec,{fromdate,todate},60);
timer function:
time= datestr(now)
ceas = timer
set(ceas,'executionMode','fixedRate')
get(ceas)
set(ceas, 'TimerFcn', 'refresh.m', 'Period', 5) %run the timer every X seconds and execute the --disp(rand)--
get(ceas)
startat(ceas, '04:04:00')
Any idea how to do it? thank you in advance

Réponse acceptée

Geoff Hayes
Geoff Hayes le 11 Juin 2016
G - since the timer callback (that function that you wish to execute every sixty seconds) is a function, then you need to assign a function handle rather than a string giving the name of the file that you wish to execute. Perhaps this is different for your version of MATLAB, but for mine (R2014a), I would do the following
set(ceas, 'TimerFcn', @refresh, 'Period', 60)
where, in my refresh.h file I would have
function refresh(source, eventdata)
fromdate = now-1;
todate = now;
%timeseries(c,sec,{fromdate,todate},60);
For all timer callbacks, there are two default input parameters, source and eventdata, so you need to define them as inputs to the function.
In the above, I've commented out the call to timeseries because it is unclear what c and sec are (or why you are passing a cell array into timeseries). What is the intent here? Where are these variables (c and sec) coming from?
  12 commentaires
Geoff Hayes
Geoff Hayes le 17 Juin 2016
Greg - you can use evalin to evaluate certain commands in the workspace...though this is sometimes discouraged. With this, you should be able to get your IQFeedTimeseriesData from the base workspace as
IQFeedTimeseriesData = evalin('base','IQFeedTimeseriesData');
You can then use assignin to assign/set your REALTIMEDATA1 to the workspace as
assignin('base', 'REALTIMEDATA1', REALTIMEDATA1);
G
G le 17 Juin 2016
Yes.. as i can see , it runs a little slow. Anyway thank you for the apportation, you're a genius !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by