Exporting value to .csv file with updated value from a timer

I have a timer which updates a value in my script every minute
Period = 60; % Update period in seconds
tim = timer('Period', Period, 'ExecutionMode', 'fixedRate',...
'TimerFcn', 'MyScript');
start(tim)
stop(tim)
I want to store the updated value in the next row (same column) of a .csv file every time the timer resets
%Writes the current value to 60 mins.csv in row 1, column 1
csvwrite('60 mins.csv',MyValue,0,0);
My guess is that i need a counter to increase the row number with each updated value/timer iteration. I want to collect at least 1440 values (a full day).
Any thoughts?
Note: the timer IS supposed to leave the whole script on an infinite loop, just in case anybody was wondering.

Answers (1)

A couple of thoughts:
  • see Running Script using Timer and note the comment by Alfonso Nieto-Castanon
  • an alternative to cswrite is fopen( ..., 'a') together with fprintf. No need for a counter; it appends to the end of the file. Or maybe dlmwrite(filename, M, '-append')
Appended:
Try
%%my_timer_test
tmr = timer ...
( 'Name' , 'my_timer' ...
, 'TimerFcn' , @my_fcn ...
, 'BusyMode' , 'drop' ...
, 'ExecutionMode' , 'fixedRate' ...
, 'Period' , 6 ...
, 'StartDelay' , 1 ...
);
start( tmr )
where
function my_fcn( tmr, data )
filespec1 = 'my_fprintf_values.csv';
filespec2 = 'my_dlmwrite_values.csv';
new_value = randn(1); % stand-in for urlread( something )
fid = fopen( filespec1, 'a' );
fprintf( fid, '%f,%f\n', now, new_value );
fclose( fid );
M = [ now, new_value ]; % default precision not sufficient for now
dlmwrite( filespec2, M, '-append' )
end

4 Comments

Could you give an example with your alternative? I tried by extracting the length of the csv and incrementing it to store the updated value in the next position but this code only seems to print the value in the next position. It doesn't save the previous value.
%Creates a new csv and writes MyValue to a csv at position 0,0
csvwrite('60 mins.csv',MyValue,0,0);
%Read csv and get csv length
csv = csvread('60 mins.csv');
len = length(csv);
csvwrite('60 mins.csv',MyValue,len+1,0);
Any thoughts?
What I want is something like:
MyValue = 4 add to csv, timer runs new MyValue = 64 add to csv in row below, timer runs new MyValue = 38 add to csv in row below etc
My timer is only there to update MyValue because its based off real-time data.
Is MyValue updated by
  • some other process in the base workspace or
  • by code in your script?
Thought
  • it is much simpler to write with a function that takes 'append'
MyValue is determined by my script. The script downloads a csv file from a URL every time the script is run. The URL data is just a single value in a csv file which causes MyValue to change. The timer just re-runs the whole script every minute so that the URL data is downloaded again and a new MyValue is calculated from the new URL data. I want to append my own CSV file so that I can store the minutely changing MyValues.
The URL data is updated minutely from an external source and is basically a single cell value in a csv file.

Sign in to comment.

Categories

Asked:

P
P
on 12 Dec 2013

Commented:

on 12 Dec 2013

Community Treasure Hunt

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

Start Hunting!