rcty
rcty

Reputation: 737

How to extract two columns of data from a text while skipping certain lines between the data in MATLAB?

I get my data as given below (refer to the large data). I need to extract the rows of data after the 19th line the text file. For doing that I can use the following code.

filename= ['f.rpt'];

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

However, this will only read and extract the data till the lines containing

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

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

I would like to skip those lines which do not belong to the two columns of data and extract the data from the rows below as well. The issue is, I can't figure how to skip those lines and extract just the two columns of data. The position of the intermittent data that I want to skip is not constant and changes with different outputs. Is there a way to do that?

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

Upvotes: 1

Views: 54

Answers (1)

HansHirse
HansHirse

Reputation: 18895

When using the textscan method, you can resume scanning. I incorporated that possibility in the following solution. I assumed, that you deal with numeric data, so I directly parsed the cell array output of textscan accordingly. If that is not wanted, the data appending needs to be modified.

% 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

I don't want to copy-paste the output to here, because of its length. Please have a look yourself.

Upvotes: 1

Related Questions