【问题标题】:octave/matlab read text file line by line and save only numbers into matrixoctave/matlab 逐行读取文本文件并仅将数字保存到矩阵中
【发布时间】:2016-08-10 06:46:47
【问题描述】:

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

                     "Surface Integral Report"

       Mass-Weighted Average
          Static Temperature                  (k)

          crossplane-x-0.001            1242.9402
          crossplane-x-0.025            1243.0017
          crossplane-x-0.050            1243.2036
          crossplane-x-0.075            1243.5321
          crossplane-x-0.100            1243.9176

我想使用 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);

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

【问题讨论】:

  • 我认为 grep 或 awk 更适合这个任务
  • 谢谢安迪,我用过 sed ...

标签: octave


【解决方案1】:

这是您特定文件的快速方法:

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

【讨论】:

    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多