如何从文本中提取两列数据,同时在MATLAB中跳过数据之间的某些行?

问题描述 投票:1回答:1

我得到下面给出的数据(参考大数据)。我需要在文本文件的第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
1个回答
1
投票

使用textscan方法时,你可以使用resume scanning。我将这种可能性纳入以下解决方案中。我假设你处理数值数据,所以我直接解析了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

我不想将输出复制粘贴到此处,因为它的长度。请自己看看。

© www.soinside.com 2019 - 2024. All rights reserved.