How to save output from a timer that callback a function

8 次查看(过去 30 天)
At the moment, I am working with a timer, which should run a function every half an hour. I am facing several troubles in saving the data generated by the function. The aim of my function (timerCallback) is to read some data from a website and record them into a vector called 'output'. Here is my function:
function [output,b,vars]=timerCallback(obj,event,fullURL)
fullURL = ['http://realtimeweb-prod.nationalgrid.com/SystemData.aspx'];
str = urlread(fullURL);
expression = '<span id="ContentPlaceHolder1_lblScoEng">(.*)</span>MW';
[tokens,matches] = regexp(str,expression,'tokens','match');
p=str2double(tokens{1}{1});
c=clock;
output=[c(1:5),p];
set(obj,'UserData',output);
end
I'd like to callback this function each half an hour and record the new data in a matrix, in order to have an history of this data.
Here I define my timer:
t=timer('TimerFcn',{@timerCallback},...
'ExecutionMode','fixedRate',...
'TasksToExecute',Inf);
The timer works, the command
set(obj,'UserData',output);
works as well, so the UserData of the timer is updated every time.
What I am not able to do is to save this updated result in my Workspace (and then, as final step, to build the matrix with all the recorded data).
If I use the command:
get(obj,'UserData')
I am only able to get the latest data recorded. I think that I need to write a code able to repeat this command every time that the timer run the function.
Any suggestions are more than welcome.
Ilaria
  3 个评论
Ilaria Di Fresco
Ilaria Di Fresco 2016-10-14
Hi Mostafa,
Thanks a lot for your answer!
Unfortunately, I've just read it this morning and yesterday night I found a solution that fit my problem. I saved my output in a text file.
function [output]=timerCallback(obj,event,fullURL)
fullURL = ['http://realtimeweb-prod.nationalgrid.com/SystemData.aspx'];
str = urlread(fullURL);
expression = '<span id="ContentPlaceHolder1_lblScoEng">(.*)</span>MW';
[tokens,matches] = regexp(str,expression,'tokens','match');
p=str2double(tokens{1}{1});
c=clock;
output=[c(1:5),p];
fileID = fopen('Scotland-England.txt','at');
fprintf(fileID,'%d %d %d %d %d %d \n',output);
fclose(fileID);
end
Using 'at' (instead of 'w') I am able to write the new output without deleting the previous one.
Thanks
Ilaria
raym
raym 2018-3-4
in the function ,first get(obj,'UserData'), then combine with the new output set(obj,'UserData',[get(obj,'UserData'),output]);

请先登录,再进行评论。

回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by