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
2016년 6월 11일
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
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?
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)
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.)
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!
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
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?
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');
assignin('base', 'REALTIMEDATA1', REALTIMEDATA1);
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 Exchange에서 Function Creation에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
