What is the equivalent most efficient way to tail -n 1 file.csv in matlab

3 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Dec 2024
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:
  • 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
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 Comments
Walter Roberson
Walter Roberson on 12 Dec 2024
Unless, that is, you have sequences of emoji. Apparently fully specifying some emoji takes 7 bytes.

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!