Time function. Execute a script every X seconds

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

 採用された回答

Geoff Hayes
Geoff Hayes 2016 年 6 月 11 日

0 投票

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 件のコメント

G
G 2016 年 6 月 12 日
Hi Geoff,
Many thaks for your feed-back.
I am also using matlab2014. In my script, c is a datafeed connection that i establish between matlab and my data provider
if true
c = iqf('login','pass', 'Admin');
end
and sec is the stock data/share prices that i want to download
if true
sec = 'EURUSD.FXCM';
end
In this case i want to download EURUSD data.
The solution that you proposed it seems that is partially working. The cause is that after the first timer refresh (first 60 seconds) i get the following error message :
_ _ _ Parameter name: asyncResult Source: System HelpLink: _Warning: Error occurred while executing delegate callback: Message: The IAsyncResult object was not returned from the corresponding asynchronous method on this class.____
do you know what it can be?
thanks in advance Greg
Geoff Hayes
Geoff Hayes 2016 年 6 月 12 日
Hi Greg - are you only instantiating the c once or are you doing it every time the timer fires?
G
G 2016 年 6 月 12 日
Hi Geoff,
I am using the time.m script and the refresh.m function (please see attached)
Yes the c (connection) is inside of the refresh function. Really conecting just 1 time is enough but if i delete the c from my refresh function, the function is not runing properly * at leas i don't know how to redact it correctly)
Geoff Hayes
Geoff Hayes 2016 年 6 月 12 日
Greg - you may want to consider nesting your refresh function within the parent "main function" so that it has access to the local variables defined within. Then, you can open the connection C there (in the parent) yet still make use of it within the nested child. Suppose you create an m file named myMainFunc.m and define the main function (rather than creating a script) as
function myMainFunc
time= datestr(now);
ceas = timer;
set(ceas,'executionMode','fixedRate');
get(ceas);
set(ceas, 'TimerFcn', @refresh, 'Period', 60);
get(ceas);
startat(ceas, '08:18:10'); %Start to run the timer
% stop(ceas)
c = iqf('login','pass', 'Admin');
sec = 'EURUSD.FXCM';
function refresh(source, eventdata)
fromdate = now-4;
todate = now;
timeseries(c,sec,{fromdate,todate},60);
end
end
Since referesh is nested within myMainFunc then it has access to c and sec, so you can avoid the repeated calls to open (?) the connection via iqf. (Perhaps the problem before was that the connection was never being closed before you tried to open it again.)
See nested functions for more information on this topic.
Geoff Hayes
Geoff Hayes 2016 年 6 月 12 日
I would also rename your refresh function as it conflicts with a built-in one that I have
/Applications/MATLAB_R2014a.app/toolbox/matlab/graphics/refresh.m
Geoff Hayes
Geoff Hayes 2016 年 6 月 14 日
G's answer moved here
Thank you Geoff,
I just tested the function with the real-time data and it work perfect !!
Many thanks for your aportation and help, Great man! Kind regards, Greg
Geoff Hayes
Geoff Hayes 2016 年 6 月 14 日
Glad to have been able to help, Greg!
G
G 2016 年 6 月 16 日
編集済み: Geoff Hayes 2016 年 6 月 16 日
Hi Geoff, how are you doing ? Hope everything well!!
One question, if I want to work with a variable from my workspace IQFeedTimeseriesData and export the result of my function REALTIMEDATA1 also on my workspace, how can I do it?
function myMainFunc2
time= datestr(now);
ceas = timer;
set(ceas,'executionMode','fixedRate');
get(ceas);
set(ceas, 'TimerFcn', @refresh2, 'Period', 1);
get(ceas);
startat(ceas, '21:26:00'); %Start to run the timer
% stop(ceas)
function refresh2(source, eventdata)
RT = IQFeedTimeseriesData(:,1);
outRT=datevec(RT,'yyyy-mm-dd HH:MM:SS');
data = cellfun(@str2num,IQFeedTimeseriesData(:,2:end));
RealTimeData = [data(:,1:4) , data(:,6), outRT(:,4)];
REALTIMEDATA1 = flipud(RealTimeData);
end
end
thank you in advance greg
Geoff Hayes
Geoff Hayes 2016 年 6 月 16 日
Hi Greg - by workspace, do you mean your base workspace? Why not just pass this variable into your main function above? Or is it changing through some other process?
G
G 2016 年 6 月 17 日
編集済み: G 2016 年 6 月 17 日
Hi Geoff,
Yes, by workspace i mean the base workspace. It is changing through some other process. I will need the variable REALTIMEDATA1 on my workspace to make a real-time forecast of some values using a neural network.
please tell me if i was clear
Geoff Hayes
Geoff Hayes 2016 年 6 月 17 日
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 2016 年 6 月 17 日
Yes.. as i can see , it runs a little slow. Anyway thank you for the apportation, you're a genius !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

質問済み:

G
G
2016 年 6 月 11 日

コメント済み:

G
G
2016 年 6 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by