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

 채택된 답변

Benjamin Kraus
Benjamin Kraus 2023년 2월 21일
편집: Benjamin Kraus 2023년 2월 21일

0 개 추천

@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.
One quick and dirty way to do this is using assignin (the last thing listed on 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
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.
roberto
roberto 2023년 2월 22일
tks very much Benjamin ,
the code works and I don't mind if tbl is overwritten , this is exactly what I want, since tbl every 60 sec. returns refreshed data.
tks to Walter, your suggestion about backgroundPool is useful.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 20일

0 개 추천

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)
Total time with [tic...toc]: 10.636126

댓글 수: 3

roberto
roberto 2023년 2월 20일
I just want to refresh webread every 60 sec. can you rewrite the code? tks
roberto
roberto 2023년 2월 20일
편집: roberto 2023년 2월 20일
anyway I've tried but it works just once and stops. webread doesn't refresh
Walter Roberson
Walter Roberson 2023년 2월 20일
pause(60) to delay 1 minute.

댓글을 달려면 로그인하십시오.

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 20일

0 개 추천

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

Walter Roberson
Walter Roberson 2023년 2월 20일
If you are going to run a timer, have the invoked function do the webread. Timer period 60, and you can set a TasksToExecute if you want a limited period.
roberto
roberto 2023년 2월 20일
So how do you write the code? Tks
URL = 'https://www.mathworks.com'; %adjust as appropriate
t = timer('TimerFcn', @(src,event)do_timer_stuff(src,event,URL), 'Period', 60, 'ExecutionMode', 'fixedspacing');
start(t)
function do_timer_stuff(src, event, URL)
DT = webread(URL);
%now do something with the data you read
end
roberto
roberto 2023년 2월 21일
It doesn't work:
"function do_timer_stuff(src, event, URL)
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files."
Walter Roberson
Walter Roberson 2023년 2월 21일
Store the code in a file and execute the file.
roberto
roberto 2023년 2월 21일
I've tried but:
Error while evaluating TimerFcn for timer 'timer-20'
Execution of script webread as a function is not supported:
/MATLAB Drive/webread.m
@roberto: you have created your own file named WEBREAD:
/MATLAB Drive/webread.m
Rename that file to something else (not the name of an inbuilt MATLAB function).
roberto
roberto 2023년 2월 21일
편집: roberto 2023년 2월 21일
it runs and create in workspace a t timer and a file URL. But I don't need the URL, I need the table (ans) that I get in return when I launch manually the webread on command window. tks
roberto
roberto 2023년 2월 21일
any suggestion?
Walter Roberson
Walter Roberson 2023년 2월 21일
https://www.mathworks.com/help/matlab/matlab_prog/share-data-between-workspaces.html
But remember you are configuring the timer to run indefinitely. You could store each result in a cell array, but your code is going to continue executing after you start() the timer. There is nothing that is sitting around waiting for new content to be added to the cell.
If what you want is to continually display the current content of the page then you should do that work inside the timer callback function.
roberto
roberto 2023년 2월 21일
편집: roberto 2023년 2월 21일
sorry, but is there a way to simply launch webread on command window, every 60 secs in automatic? this is all I need, because webread, everytime is manually launched in command window, returns a refreshed table in workspace so I can analyze it.
I hope you can help. tks
Walter Roberson
Walter Roberson 2023년 2월 21일
No there is not.

댓글을 달려면 로그인하십시오.

roberto
roberto 2023년 2월 21일
편집: roberto 2023년 2월 21일

0 개 추천

with little changes the code works and refreshes on command window.
ans =
1000×5 table (this is the answer on command window).
but the problem is that the workspace is empty..no ans in table format.
how add the table (ans) from command window to workspace?
here the code to call webread:
function doStuff()
intervalInS=60;
everyNowAndThen = timer("Period",intervalInS,"ExecutionMode","fixedRate","TimerFcn",@refresh);
everyNowAndThen.start();
function refresh(~,~)
webread('https://api........CSV')
end
end

댓글 수: 1

roberto
roberto 2023년 2월 21일
편집: roberto 2023년 2월 21일
any kind help?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Downloads에 대해 자세히 알아보기

태그

질문:

2023년 2월 20일

댓글:

2023년 2월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by