Running Script using Timer

I am trying to use Timer function to run a script at pre-specified interval (30 seconds). However, most of the examples, including MATLAB help, is directed towards using callback functions. However, when I tried something similar to use the script to run using timer, I get an error message.
Specifically, I am using Bloomberg's data function to get real time data and would like to simply revoke that Bloomberg function every 30 seconds.
Any help would be sincerely appreciated. Please feel free to ask question, if I am not being clear.
Thank you,

回答 (2 件)

per isakson
per isakson 2013 年 12 月 5 日
編集済み: per isakson 2013 年 12 月 6 日

1 投票

The trick is to use an anonymous function. Try
>> my_timer_test
2013-12-05 22:09:31
2013-12-05 22:09:43
2013-12-05 22:09:55
2013-12-05 22:10:07
where
%%my_timer_test
tmr = timer ...
( 'Name' , 'my_timer' ...
, 'TimerFcn' , @(x,y) my_script ...
, 'BusyMode' , 'drop' ...
, 'ExecutionMode' , 'fixedDelay' ...
, 'Period' , 12 ...
, 'StartDelay' , 1 ...
);
start( tmr )
and
%%my_script
disp( datestr( now, 31 ) )
are two script files

8 件のコメント

KKR
KKR 2013 年 12 月 6 日
編集済み: per isakson 2016 年 9 月 30 日
I tried your example. However, when I put my code in there, it gives me some errors:
here is what I have as my_script
tend = datenum(year(now),month(now),day(now),hour(now),minute(now),0);
tst = datenum(year(now),month(now),day(now),hour(now),minute(now)-20,0);
d = timeseries(c,'ESZ3 Index',{tst, tend},5,'Trade');
I get the following error: Attempt to add "tend" to a static workspace. See MATLAB Programming, Restrictions on Assigning to Variables for details.
Any idea?
Thank you very much for your help.
per isakson
per isakson 2013 年 12 月 6 日
Try to use the {}Code button. It makes it easier to read the code.
per isakson
per isakson 2013 年 12 月 6 日
編集済み: per isakson 2013 年 12 月 6 日
It is obviously not a good idea to use a script in the value of the property, TimerFcn. See Variables in Nested and Anonymous Functions and note especially: "Calling a MATLAB script that creates a variable". The property name contains the letters Fcn for a reason.
Thus, convert your script to a function.
Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2013 年 12 月 7 日
I imagine you probably want to run your script on the base workspace (as if you run it on the Matlab command window), right?.
If that is the case, in the example above simply change the portion that reads:
@(x,y)my_script
into:
@(varargin)evalin('base','my_script');
Hope this helps
KKR
KKR 2013 年 12 月 19 日
in conjunction with previous issue with Timer, I ran into one more problem, hope you will be able to help me out:
I wrote another piece of script and I tried using Timer function, but second time it iterates through it - it gives me the following error message:
Error while evaluating TimerFcn for timer 'my_timer'
Java exception occurred: com.bloomberglp.blpapi.InvalidConversionException: Cannot convert String to Enumeration
Do you have any idea what is going on here? I can post the full error message, if you think that might be helpful. I sincerely appreciate your help.
Kaushal
per isakson
per isakson 2013 年 12 月 19 日
Matlab is not always helpful! I guess the Blomberg page contained something else than digits in a field where "your" script expected digits.
See:
KKR
KKR 2013 年 12 月 19 日
Somehow it gives me an error - every time it goes to second iteration - first time invoke works perfectly fine.
per isakson
per isakson 2013 年 12 月 19 日
What does "it" refer to? Isn't it a case for ordinary debugging? http://undocumentedmatlab.com/ may have something useful to say on debugging Java code.

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

KKR
KKR 2013 年 12 月 7 日

1 投票

That worked.
Sorry about inconvenient code description.
Thank you very much for the help.

カテゴリ

ヘルプ センター および File ExchangePlatform and License についてさらに検索

質問済み:

KKR
2013 年 12 月 5 日

編集済み:

2016 年 9 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by