在 MATLAB 中从末尾开始读取文件
我想知道是否有人知道如何在 MATLAB 中打开和读取文件,从文件末尾开始读取。该文件不断更新(在读取之间以某种非恒定速率),我想每次读取文件的最后六行。
我还想包括一个测试来验证我不会重复阅读相同的行两次。每行的格式如下(每个变量都是浮点数):
timestamp accx accy accz gyrox gyroy gyroz magx magy magz
我试图使用 fseek 将位置更改为文件的最后一行,但这只允许我读取我认为文件的最后一行,除非我指定一定数量的字节,否则不会向后读取文件,而我无法确切知道字节数。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是基于 UNIX 的系统(Linux/Mac),则可以直接使用系统命令来执行您想要的操作。下面是一个示例测试文件:
您可以在 unix 上使用
tail
读取它,并使用system
命令直接将其读取到 MATLAB 中。将
-n 2
中的2
替换为您想要读取的行数。接下来,为了确保您读取同一行,您可能需要存储时间戳(第一列)。最简单的方法是再次让 unix 为您做这件事,
使用
str2num
将其转换为数字,并在每次读取时存储这些数字,然后使用函数ismember
检查是否新的时间戳已经是您之前读取的时间戳的一部分。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
tail
on unix and into MATLAB directly using thesystem
command.Replace the
2
in-n 2
with 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
str2num
and store these each time you read and then use the functionismember
to check if a new timestamp is already part of your previously read timestamps.了解您正在从文件中读取哪一行的唯一方法是从头开始并计算换行数。没有办法从文件末尾开始直接向后查找一定行数。
您可以编写一个函数,从文件末尾向后读取,直到看到 N 个换行符,然后输出该块。这正是
tail
的工作原理。这是来自 GNUtail.c< 的评论/code>
:
如果文件不太大,最简单的方法是编写一个一次读取一行的函数,只保留最后 N 行。
The only way to know what line you are reading from a file is to start at the beginning and count newlines. There is no way to start at the end of the file and directly seek backwards by a certain number of lines.
You could write a function that reads backwards from the end of the file until it sees N newlines, and then outputs this chunk. This is precisely how
tail
works. Here is a comment from GNUtail.c
:If the file is not too huge, the easiest thing to do would be to write a function that reads a line at a time, only keeping the last N lines.
您可以搜索文件的末尾块(使用
fseek()
在 6 行之前设置 EOF 的位置,然后搜索最后 6 或 7 个换行符并使用find()
然后您可以提取数据,因为您知道最后 6 行在您读取的原始块中的位置。You could search the end chunk of the file (using
fseek()
to set the position for the EOF well in advance of your 6 lines and then search for the last 6 or 7 newline characters and withfind()
. Then you could extract your data because you know where the last 6 lines are in the original chunk that you read.