MATLAB:如何读取文本文件的每一行? [英] MATLAB: how to read every Nth line of a text file?

查看:1370
本文介绍了MATLAB:如何读取文本文件的每一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  dtau E_av方差N_sims时间
0.001 0.497951 0.000211625 25太阳Apr 3 18:18:12 2011

dtau E_av variance N_sims Time
0.002 0.506784 0.000173414 25 Sun Apr 3 18:18:58 2011

现在我想使用textscan将每三行的前四列(除时间外)读入MATLAB;在使用 fid = fopen('data.text')之后,我基本上必须循环:

  results = textscan(fid,'%f%f%f%f',1,'headerlines',1); 

有什么想法?
干杯!

解决方案

  fid = fopen('data.text') 
while feof(fid)
results = textscan(fid,'%f%f%f%f',1,'headerlines',1);
//正在处理...

for i = 1:2
fgets(fid)
end
end

fgets 直到行尾,并返回该行的文本。所以,只需调用它两次跳过两行(放弃函数的返回值)。

I have some data that's formatted as follows:

dtau     E_av        variance    N_sims      Time
0.001   0.497951    0.000211625 25      Sun Apr  3 18:18:12 2011

dtau     E_av        variance    N_sims      Time
0.002   0.506784    0.000173414 25      Sun Apr  3 18:18:58 2011

Now I want to read the first 4 columns (anything but the time) of every third line into MATLAB using textscan; after using fid = fopen('data.text'), I basically have to loop this:

results = textscan(fid, '%f %f %f %f', 1,'headerlines',1);

Any ideas? Cheers!

解决方案

fid = fopen('data.text')
while ~feof(fid)
  results = textscan(fid, '%f %f %f %f', 1,'headerlines',1);
  //Processing...

  for i = 1:2
    fgets(fid)
  end
end

fgets reads until the end of a line, and returns the text on that line. So, just call it twice to skip two lines (discarding the return value of the function).

这篇关于MATLAB:如何读取文本文件的每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆