function plus timer in background

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 Comment

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");

Sign in to comment.

 Accepted Answer

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 Comments

yes you are right I should also insert this second script into the first function with timer . I've tried but didn't succed. so how should I modify the first function (written below)? Thank you very much
function doStuff()
intervalInS=60;
everyNowAndThen = timer("Period",intervalInS,"ExecutionMode","fixedRate","TimerFcn",@refresh);
everyNowAndThen.start();
function refresh(~,~)
assignin('base','tbl',webread('https://api.=CSV'))
end
end
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
works like a charm, simply and efficient.
tks very much Walter.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 25 Feb 2023

Commented:

on 25 Feb 2023

Community Treasure Hunt

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

Start Hunting!