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!