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

 Accepted Answer

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 Comments

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
Hi Greg - are you only instantiating the c once or are you doing it every time the timer fires?
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)
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.
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
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
Glad to have been able to help, Greg!
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
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 on 17 Jun 2016
Edited: G on 17 Jun 2016
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
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);
Yes.. as i can see , it runs a little slow. Anyway thank you for the apportation, you're a genius !

Sign in to comment.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Asked:

G
G
on 11 Jun 2016

Commented:

G
G
on 17 Jun 2016

Community Treasure Hunt

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

Start Hunting!