首页 > 解决方案 > 如何在MATLAB中跳过数据之间的某些行时从文本中提取两列数据?

问题描述

我得到我的数据如下(参考大数据)。我需要提取文本文件第 19 行之后的数据行。为此,我可以使用以下代码。

filename= ['f.rpt'];

fid = fopen(filename);
A =  textscan(fid, '%f %f','HeaderLines',19) ;
b = A{2};
fclose(fid);   

但是,这只会读取和提取数据,直到包含

Field Output reported at nodes for part: PART-2-1

            Node       CSMAXSCRT
           Label          @Loc 1
---------------------------------

我想跳过那些不属于两列数据的行,并从下面的行中提取数据。问题是,我不知道如何跳过这些行并仅提取两列数据。我要跳过的间歇性数据的位置不是恒定的,并且会随着不同的输出而变化。有没有办法做到这一点?

********************************************************************************
Field Output Report

Source 1
---------

   ODB: ffffff
   Step: Step-1
   Frame: Increment      7600: Step Time =   6.0000E-011

Loc 1 : Nodal values from source 1

Output sorted by column "CSMAXSCRT General_Contact_Domain".

Field Output reported at nodes for part: PART-1-1

            Node       CSMAXSCRT
           Label          @Loc 1
---------------------------------
               1              1.
               2              1.
               3              1.
               4              1.
             456              1.
             455              1.
             454              1.
             453              1.
             452              1.
             451              1.
             450              1.
              12              1.
              13              1.
              14              1.
              15              1.
              16              1.
              17              1.
              18              1.
              19              1.
              20              1.
              21              1.

   Field Output reported at nodes for part: PART-2-1

            Node       CSMAXSCRT
           Label          @Loc 1
---------------------------------
               1              1.
               2              1.
               3              1.
               4              1.
               5              1.
               6              1.
               7              1.
               8              1.
               9              1.
              10              1.
              11              1.
              12              1.
              13              1.
              14              1.
              15              1.
              16              1.
              17              1.
              18              1.
              19              1.
              20              1.
              21              1.
              22              1.
              23              1.

   Field Output reported at nodes for part: PART-3-1

            Node       CSMAXSCRT
           Label          @Loc 1
---------------------------------
               1              1.
               2              1.
               3              1.
               4              1.
               5              1.
               6              1.
               7              1.
               8              1.
               9              1.
              10              1.
              11              1.
              12              1.
              13              1.
              14              1.
              15              1.
              16              1.
              17              1.
              18              1.
              19              1.
              20              1.
              21              1.
              22              1.
              23              1.   

标签: matlab

解决方案


使用该textscan方法时,可以恢复扫描。我在以下解决方案中加入了这种可能性。我假设您处理的是数字数据,因此我直接相应地解析了元胞数组输出textscan。如果不需要,则需要修改附加的数据。

% Read in whole text.
text = fileread('f.txt');

% Initialize result array.
C = [];

% Initialize absolute cursor position.
absPos = 0;

% While absolute cursor position hasn't reached EOF.
while (absPos < numel(text))

  % First read -> Skip 19 header lines.
  if (absPos == 0)
    [data, relPos] = textscan(text, '%f %f', 'HeaderLines', 19);
    absPos = absPos + relPos;

  % Every further read -> Skip 5 intermediate header lines.
  else
    [data, relPos] = textscan(text((absPos+1):end), '%f %f', 'HeaderLines', 5);
    absPos = absPos + relPos;
  end

  % Append data.
  C = [C; [data{:}]];
end

由于它的长度,我不想将输出复制粘贴到这里。请你自己看看。


推荐阅读