2

我有一些格式如下的数据:

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);

有任何想法吗?干杯!

4

2 回答 2

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读取到一行的末尾,并返回该行的文本。因此,只需调用它两次即可跳过两行(丢弃函数的返回值)。

于 2011-04-03T17:27:08.273 回答
0

由于您知道您将有 5 个列标签(即字符串),后跟 4 个数值,然后是 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
于 2011-04-04T14:49:23.137 回答