2011-04-03 139 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 
相關問題