MATLAB Answers

How to read text file backwards?

23 views (last 30 days)
Ian Yuan
Ian Yuan on 2 Dec 2020
Answered: Walter Roberson on 3 Dec 2020
Hi,
I'm streaming data into a text file that updates every second that looks like this:
12/2/2020 08:02:02 x1
12/2/2020 08:02:03 x2
12/2/2020 08:02:04 x3
...
I would like to read only the last 10 x values of the text file to find the average value of them, without reading in the entire file. Is there a way to do that?
I'm on a mac os platform.
Thank you,

  0 Comments

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 3 Dec 2020
You can fopen() the file for reading and fseek() to a particular offset relative to the end of file.
You will not be able to fseek to exactly the beginning of a line, because no modern filesystem stores files by lines: all modern filesystems store as a stream of bytes, and we can see that your lines are not a consistent number of bytes (even if your data readings are all exactly the same size, you are using a date format that does not put in leading 0 on the day number, so next week the entries like 12/2/2020 08:02:02 x1 would start looking like 12/10/2020 08:02:02 x1 which is a different number of bytes per line.)
So what you should do is fseek() backwards by 10+1 = 11 times the maximum size you expect per line. Then having done that, fgets() or fgetl() and discard that . Most of the time you would have ended up in the middle of a line, and the fgets() or fgetl() will read to the end of the line you ended up in, and you discard that incomplete line. Now you are positioned to the beginning of a line and have at least 10 complete lines of data. Read those lines. If you ended up with more than 10 readings, discard the extras from the beginning.
But be careful: since you are on Mac, when you are in the middle of reading, the file writer might be in the middle of writing, so the last entry you read could potentially be an incomplete line. (It depends exactly how the writer writes lines. It is possible for a writer to arrange that writing each line is an "atomic operation", making it impossible for readers to get partial lines. But the writer would have to specifically program that.)
To re-emphasize: not only MATLAB but MacOS, Windows, and Linux and the international standards for C and C++ and POSIX do not provide any way of positioning by lines (not without having pre-indexed the data to locate the positions of the lines.)
This is not a bug in MATLAB: this is fundamental to the way that files have been stored on computers since the early 1980s.

  0 Comments

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by