2011-04-03 362 views
2

我的影片格式的一些数据如下:MATLAB:如何读取文本文件的每一行第N行?

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 

现在我想用textscan阅读每隔二行的前4列(任何东西,但时间)到MATLAB;使用fid = fopen('data.text')后,我基本上必须循环:

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

任何想法? 干杯!

回答

2
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直到一行的结尾,并返回该行的文本。所以,只需调用它两次跳过两行(放弃函数的返回值)。

+1

你的实现可能仍然打破,这取决于在该文件中的行数。谢谢 – eat 2011-04-03 17:54:51

0

既然你知道你将不得不其次是4个数值5个标签(即字符串),其次是5个字符串(例如,'Sun''Apr''3''18:18:12''2011'),你其实可以读取所有的数字数据的成单个N乘4矩阵与一个呼叫到TEXTSCAN

fid = fopen('data.text','r');     %# Open the file 
results = textscan(fid,[repmat(' %*s',1,5) ... %# Read 5 strings, ignoring them 
         '%f %f %f %f' ...  %# Read 4 numeric values 
         repmat(' %*s',1,5)],... %# Read 5 strings, ignoring them 
        'Whitespace',' \b\t\n\r',... %# Add \n and \r as whitespace 
        'CollectOutput',true);  %# Collect numeric values 
fclose(fid);          %# Close the file 
results = results{1};       %# Remove contents of cell array 
相关问题