How to save output from a timer that callback a function

8 Ansichten (letzte 30 Tage)
Ilaria Di Fresco
Ilaria Di Fresco am 12 Okt. 2016
Kommentiert: raym am 4 Mär. 2018
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 Kommentare
Ilaria Di Fresco
Ilaria Di Fresco am 14 Okt. 2016
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 am 4 Mär. 2018
in the function ,first get(obj,'UserData'), then combine with the new output set(obj,'UserData',[get(obj,'UserData'),output]);

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by