Search code examples
matlabfiletextstructureread-data

How to read a structured text file and create a structure for it in Matlab?


I have a text file which contains results for many time steps. for each time step, some basic info was saved in the first line, followed by a matrix that contains other data for this step. The matrix size can differ at every time step and it is not predefined.

How to create a structure based on such text file?

Thanks in advance!

P.S. the results in text file looks like this:

 time=   4.3750000000000001E-004           3           7           4           1   4.9999989999999998E-004
  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00
  0.0000E+00  0.0000E+00  5.6569E+08  7.5717E+08  5.6569E+08  0.0000E+00  0.0000E+00
  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00
  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00

 time=   5.0000000000000001E-004           3           5           3           0   4.9999989999999998E-004
  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00
  0.0000E+00  0.0000E+00  2.3593E+08  0.0000E+00  0.0000E+00
  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00  0.0000E+00

 time=   1.8125000000000001E-003           3           3           3           1   1.8749999000000001E-003
  0.0000E+00  0.0000E+00  0.0000E+00
  0.0000E+00  3.9138E+07  0.0000E+00
  0.0000E+00  0.0000E+00  0.0000E+00

Solution

  • Assume each new set for data starts by time= and ends by spaced line, you can use the following code sample:

    %Open text file for reading (assume file name is 'Data.txt').
    f = fopen('Data.txt', 'r');
    
    %Initialize main (store data) to empty matrix.
    main = [];
    
    %Initialize index to 1
    i = 1;
    
    while (~feof(f))
        %Read single line from text file (as long string).
        S = fgets(f);
    
        if (strfind(S, 'time') > 0)
            %Remove 'time= ' from the beginning of S.
            S = strrep(S, 'time=', '');
    
            %Convert string to array of numbers.
            T = sscanf(S, '%f ');
    
            %Store vector T to main(i).sub1
            main(i).sub1 = T';
    
            %Set A to empty matrix - prepare for filling with new data.
            A = [];
    
            %Read next line from text file (as long string).
            S = fgets(f);        
        end    
    
        %Convert string to array of numbers.
        L = sscanf(S, '%f ');
    
        if (isempty(L) || feof(f))
            %Store matrix A to main(i).sub2
            main(i).sub2 = A;
    
            %Advance i (data index) by 1.
            i = i + 1;
        else
            %In case A is not empty, concatenate T to bottom of A.
            A = [A; L'];
        end
    end
    
    %Close file.
    fclose(f);
    

    Result:

    >>main(1)
    
    sub1 =
    
       4.3750e-04   3.0000e+00   7.0000e+00   4.0000e+00   1.0000e+00   5.0000e-04
    
    sub2 =
    
               0           0           0           0           0           0           0
               0           0   565690000   757170000   565690000           0           0
               0           0           0           0           0           0           0
               0           0           0           0           0           0           0
    
    
    >>main(2)
    
    sub1 =
    
       0.00050   3.00000   5.00000   3.00000   0.00000   0.00050
    
    sub2 =
    
               0           0           0           0           0
               0           0   235930000           0           0
               0           0           0           0           0
    
    
    >>main(3)
    
    sub1 =
    
       0.0018125   3.0000000   3.0000000   3.0000000   1.0000000   0.0018750
    
    sub2 =
    
              0          0          0
              0   39138000          0
              0          0          0