octave/matlab逐行读取文本文件,只将数字保存到矩阵中

octave/matlab逐行读取文本文件,只将数字保存到矩阵中,octave,Octave,我有一个关于倍频程或matlab数据后处理的问题。 我有从fluent导出的文件,如下所示: "Surface Integral Report" Mass-Weighted Average Static Temperature (k) 我想使用octave/matlab进行后处理。 如果我逐行读取第一行,只将带有“crossplane-x-”的行保存到新文件中,或者直接将这些行中的

我有一个关于倍频程或matlab数据后处理的问题。 我有从fluent导出的文件,如下所示:

                     "Surface Integral Report"

       Mass-Weighted Average
          Static Temperature                  (k)

我想使用octave/matlab进行后处理。 如果我逐行读取第一行,只将带有“crossplane-x-”的行保存到新文件中,或者直接将这些行中的数据保存到矩阵中。因为我有很多类似的文件,我可以通过调用它们的标题来制作情节。 但是我在识别包含字符“crossplane-x-”的行时遇到了麻烦。我正在尝试这样做:

clear, clean, clc;
% open a file and read line by line
fid = fopen ("h20H22_alongHGpath_temp.dat");
% save full lines into a new file if only chars inside
txtread = fgetl (fid)
num_of_lines = fskipl(fid, Inf);
char = 'crossplane-x-'
for i=1:num_of_lines,
  if char in fgetl(fid)
    [x, nx] = fscanf(fid);
    print x
  endif
endfor
fclose (fid);

有人能解释一下这个问题吗?我是否使用了正确的功能?谢谢。

这里有一个快速的方法来处理您的特定文件:

>> S = fileread("myfile.dat");       % collect file contents into string
>> C = strsplit(S, "crossplane-x-"); % first cell is the header, rest is data
>> M = str2num (strcat (C{2:end}))  % concatenate datastrings, convert to numbers
M =

   1.0000e-03   1.2429e+03
   2.5000e-02   1.2430e+03
   5.0000e-02   1.2432e+03
   7.5000e-02   1.2435e+03
   1.0000e-01   1.2439e+03

我认为grep或awk更适合这个任务谢谢你Andy,我用过sed。。。
>> S = fileread("myfile.dat");       % collect file contents into string
>> C = strsplit(S, "crossplane-x-"); % first cell is the header, rest is data
>> M = str2num (strcat (C{2:end}))  % concatenate datastrings, convert to numbers
M =

   1.0000e-03   1.2429e+03
   2.5000e-02   1.2430e+03
   5.0000e-02   1.2432e+03
   7.5000e-02   1.2435e+03
   1.0000e-01   1.2439e+03