Clear Filters
Clear Filters

Copy 60 most recent values from appended .csv to a new .csv every minute?

1 view (last 30 days)
Copy 60 most recent values from appended .csv to a new .csv every minute. MATLAB
I'd like to extract the 60 most recent values from a .csv file called MyFile which is appended with 3 new values in the row below the previous set every minute using a timer function:
dlmwrite('MyFile.csv', [MyValue,MyValue2,MyValue3], '-append');
Say I may have 150 rows of values in MyFile after 100 minutes and I want to extract the latest 60 to save to another file called MyFile2 The process happens indefinitely because of an endless timer i.e it accumulates data over time so the row size of the MyFile.csv is increasing by 1 every minute. So, 3 columns of data in each row, new row every minute.(Ignore the timer, i'm just showing here that it works and is used to get the new MyValue's) Timer:
Period = 60; % Update period in seconds
tim = timer('Period', Period, 'ExecutionMode', 'fixedRate',...
'TimerFcn', 'ThisScript');
start(tim)
stop(tim)
runtmp = fullfile('MyScriptLocation','MyScript');
run(runtmp);
How can I continually copy over the 60 most recent sets of values from the file and store them in MyFile2?
Is there a size function I could use for my .csv file so that it fetches the size of the .csv as it grows and then pulls the most recent 60 values added to it?
I feel like this would be the easiest way to do it but i'm still unsure.
Rough pseudocode i'm unsure of (don't think its right):
Take size of MyFile
for k=1:size(MyFile)
dlmwrite('MyFile2.csv', [MyValue(k),MyValue2(k),MyValue3(k)], '-append');
but obviously need code there that says subtract size from each row number so that the correct values are appended to MyFile2. Hope i've made this clear. If there aren't 60 rows of values in MyFile yet then a text/string message 'N/A' should appear.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Dec 2013
The operating systems that support MATLAB, do not have any functionality that allows the number of lines in a file to be known, short of reading the entire file from beginning to end and counting the number of lines.
  7 Comments
P
P on 29 Dec 2013
Also. the line defining col_means returns:
??? Undefined function or method 'scanf' for input arguments of
type 'char'.
Can scanf be used here?

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 Dec 2013
Can you just read the whole thing with csvread() then extract the last 60 rows?
data = csvread(filename); % Get all the data.
[rows, columns] = size(data); % Find out how many rows.
if rows >= 61
last60Rows = data(end-59:end,:);
csvwrite(newFileName, last60Rows);
end
  1 Comment
P
P on 30 Dec 2013
Thank you!!!!
Using test data this seems to be working. I will need to test it for sure tomorrow when the data I get for my script is available from its external source.

Sign in to comment.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!