- you can read everything from the beginning of the file, and throw away all except the last line
- you can start at the end of the file and seek backwards by chunks and reading each chunk, each time looking for a newline in the chunk, until eventually you find the newline or you get to the beginning of the file
What is the equivalent most efficient way to tail -n 1 file.csv in matlab
3 ビュー (過去 30 日間)
古いコメントを表示
For context we write to the .csv every test collect with general info and I have some analysis tools that grab some information from that file very consistently. I was just curious if there is something better than:
system('tail -n 1 file.csv')
built in to read the last line without traversing the entire file.
0 件のコメント
採用された回答
Walter Roberson
2024 年 12 月 11 日
There are a few cases:
If there is a maximum line length that the final line is certain not to exceed, and the characters are restricted to 8 bit, then
fid = fopen('file.csv', 'r');
fseek(fid, -(MAXIMUM_LINE_LENGTH+1), 'eof');
buffer = fread(fid, '*char*1');
%get rid of carriage returns. Get rid of trailing newlines
buffer = regexprep(buffer, {'\r', '\n+$'}, {'',''});
pos = find(buffer == newline, 1, 'last');
lastline = buffer(pos+1:end);
If there is a maximum line length that the final line is certain not to exceed, and the characters might be multibyte, then
fid = fopen('file.csv', 'r');
fseek(fid, -4*(MAXIMUM_LINE_LENGTH+1), 'eof');
buffer = fread(fid, '*char');
%get rid of carriage returns. Get rid of trailing newlines
buffer = regexprep(buffer, {'\r', '\n+$'}, {'',''});
pos = find(buffer == newline, 1, 'last');
lastline = buffer(pos+1:end);
If there is no maximum line length that the final line is certain not to exceed, then you have two choices:
Note: I coded so that trailing empty lines on the file are ignored. Otherwise you run into an existential question: if the file ends in a newline, then does that mean that there is a trailing empty line "following" the newline, or does the trailing newline mark the end of an existing line? For both Windows and Unix, the API answer is that trailing newline implies empty line afterwards: newlines are officially line seperators rather than line terminators.
4 件のコメント
Walter Roberson
2024 年 12 月 12 日
Unless, that is, you have sequences of emoji. Apparently fully specifying some emoji takes 7 bytes.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Large Files and Big Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!