フィルターのクリア

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

1 回表示 (過去 30 日間)
P
P 2013 年 12 月 27 日
コメント済み: P 2013 年 12 月 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.

採用された回答

Walter Roberson
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 件のコメント
P
P 2013 年 12 月 29 日
Also. the line defining col_means returns:
??? Undefined function or method 'scanf' for input arguments of
type 'char'.
Can scanf be used here?
Walter Roberson
Walter Roberson 2013 年 12 月 29 日
sscanf() instead of scanf()

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
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
  1 件のコメント
P
P 2013 年 12 月 30 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by