I was wondering if anyone knows how to open and read from a file in MATLAB where you begin reading from the end of the file. The file is constantly being updated (at some nonconstant rate between reads) and I want to read the last six lines of the file each time.
I would also to include a test to verify that I don’t reread the same lines twice. Each line is formatted as follows (each variable is a floating point number):
timestamp accx accy accz gyrox gyroy gyroz magx magy magz
I was trying to use fseek, to change the position to the last line of the file, but this only allows me to read the last line of the file I think, not read the file backwards unless I specify a certain number of bytes, which I wouldn’t know the number of bytes exactly.
If you’re on a unix based system (Linux/Mac), you can directly use system commands to do what you want. Here’s a sample test file:
You can read it using
tailon unix and into MATLAB directly using thesystemcommand.Replace the
2in-n 2with how many ever lines you want to read.Next, to make sure you read the same line, you might want to store the timestamps (first column). The simplest way to do that is again let unix do it for you
Convert it to numbers using
str2numand store these each time you read and then use the functionismemberto check if a new timestamp is already part of your previously read timestamps.