timer doesn't work
8 次查看(过去 30 天)
显示 更早的评论
hello everybody,with this timer I wonder why stops and don't carry on to execute. what's wrong? I want webread to be refreshed every 60 secs. tks
t = timer('TimerFcn', 'stat=false; disp(''Timer!'')',...
'StartDelay',60);
start(t)
stat=true;
while(stat==true)
webread('https://www..etc...');
pause(1)
end
0 个评论
采纳的回答
Benjamin Kraus
2023-2-21
编辑:Benjamin Kraus
2023-2-21
@Walter Roberson already pointed you to the documentation on how to Share Data Between Workspaces, which provides several ways to achieve your goal. I suggest reading that doc page.
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
The function webread('https://api........CSV') will be executed, and the result will be stored in the variable named tbl in the base workspace (i.e. workspace available at the command line).
Keep in mind that this means that as you are working, and as long as the timer is running, the variable tbl will continually be overwritten with the new data. Any other changes you make will be lost every time the timer executes.
Also keep in mind that if you are running other code when the timer is due to execute, your timer will wait until the other code is done. The reverse is also true, if your timer is running the webread command, no other MATLAB code will execute.
If your call to webread takes more than a fraction of a section, MATLAB will appear to hang every minute while webread is executing, which could be a pretty unplesant experience. I'm not sure if webread works with backgroundPool, but if you find yourself in this situation, you may want to look into backgroundPool to allow this code to run independently of your main MATLAB thread.
2 个评论
Walter Roberson
2023-2-21
timers potentially get control at the beginning of the each line of MATLAB code. But of course if you are doing a large vectorized operation or are calling a mex file it might be quite a while before the next matlab line is reached.
更多回答(3 个)
Sulaymon Eshkabilov
2023-2-20
Here it shows that it is working. An alternative way is to use [tic .. toc] to count time if this is the objective:
Tspan = 10; % Time span 10 seconds
t = timer('TimerFcn', 'stat=false; disp(''Timer!'')', 'StartDelay',Tspan);
start(t)
tic
stat=true;
while(stat==true)
webread('https://www.mathworks.com');
pause(1)
end
Tend = toc;
fprintf('Total time with [tic...toc]: %f \n', Tend)
3 个评论
Sulaymon Eshkabilov
2023-2-20
Here is how it can be attained:
Tspan = 240; % Time span 240 seconds
t = timer('TimerFcn', 'stat=false; disp(''Timer!'')', 'StartDelay', Tspan);
start(t)
Tupdated = 60;
tic
stat=true;
ii = 0;
while(stat==true)
ii=ii+1;
fprintf('WEB is read for %d time \n', ii);
DT={webread('https://www.mathworks.com')};
pause(Tupdated)
end
Tend = toc;
fprintf('Total time with [tic...toc]: %f \n', Tend)
12 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!