首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >octave/matlab逐行读取文本文件,只将数字保存到矩阵中。

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

Stack Overflow用户
提问于 2016-08-08 08:54:32
回答 1查看 6.6K关注 0票数 2

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

代码语言:javascript
复制
                     "Surface Integral Report"

       Mass-Weighted Average
          Static Temperature                  (k)
代码语言:javascript
复制
          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

我想使用八度/matlab进行后处理。如果我先逐行读取,并且只将带有“十字-x-”的行保存到一个新文件中,或者直接将这些行中的数据保存到一个矩阵中。由于我有许多类似的文件,我可以通过调用他们的标题来制作情节。但是我很难识别包含字符“十字-x-”的线条。我试着做这样的事情:

代码语言:javascript
复制
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);

有人会在这个问题上说点什么吗?我是否使用了正确的功能?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-08-09 22:46:47

以下是处理特定文件的快速方法:

代码语言:javascript
复制
>> 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38824942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档