function plus timer in background

9 次查看(过去 30 天)
Hello everybody
tks to Benjamin and Walter I run a webread function connected with a timer ,
I want to run a separate function, working in background, starting every 60 secs.
I've tried in some ways but didn't succed
this is the command:
longT=tbl(:,"dose");
LT=trenddecomp(longT);
figure(11);
plot(LT,"dose_LongTerm");
hold on;
plot(tbl,"dose");
hold off;
figure(10);
plot(LT,"dose_Seasonal")
  1 个评论
roberto
roberto 2023-2-25
移动:Walter Roberson 2023-2-25
this is my attempt.
Ii put the above command in a script called LTdose, but running the function called "longseas", it gives me the following error:
"longseas
Error while evaluating TimerFcn for timer 'MyTimer'
Unrecognized function or variable 'tbl'."
but if I launch manually the script LTdose (even with run(LTdose) ), it works as usual
please a help, tks.
function [myTimer] = longseas
myTimer = timer('Name','MyTimer', ...
'Period',30, ...
'StartDelay',0, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',@myTimerCallback);
start(myTimer);
function myTimerCallback(hObject, eventdata)
run("LTdose");

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-2-25
When you run() a script inside of a function, the workspace of the script is the workspace of the function.
When you run() a script at the command line (and you are not stopped at the debugger), the workspace of the script is the base workspace.
The earlier discussion involved writing a timer not shown here, in which you wanted data read in every 60 seconds, and the final version of the code was assigning the data into the base workspace. If that is still what you are doing then in your script you should evalin('base', 'tbl') to retrieve tbl from the base workspace.
I am not clear as to why you want two separate 60 second timers, instead of having the callback for the first timer run the script?
  3 个评论
Walter Roberson
Walter Roberson 2023-2-25
function doStuff()
intervalInS=60;
everyNowAndThen = timer("Period",intervalInS,"ExecutionMode","fixedRate","TimerFcn",@refresh);
everyNowAndThen.start();
end
function refresh(~,~)
tbl = webread('https://api.=CSV');
assignin('base','tbl',tbl);
run("LTdose");
end
roberto
roberto 2023-2-25
works like a charm, simply and efficient.
tks very much Walter.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by