Copy 60 most recent values from appended .csv to a new .csv every minute?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Walter Roberson
2013-12-27
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 个评论
更多回答(1 个)
Image Analyst
2013-12-29
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!