Filter löschen
Filter löschen

function plus timer in background

8 Ansichten (letzte 30 Tage)
roberto
roberto am 25 Feb. 2023
Kommentiert: roberto am 25 Feb. 2023
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 Kommentar
roberto
roberto am 25 Feb. 2023
Verschoben: Walter Roberson am 25 Feb. 2023
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");

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 25 Feb. 2023
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 Kommentare
Walter Roberson
Walter Roberson am 25 Feb. 2023
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 am 25 Feb. 2023
works like a charm, simply and efficient.
tks very much Walter.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by