mwoua
mwoua

Reputation: 403

How to load/import a text file with strange format in matlab

I have to extract data from a text file with some weird format with matlab but I cannot do it since there is much text and the functions I know do not work in this case. The data is arranged in different sets (more than 200) of some 2000 values each.

Here is how is formatted the data in the text file :

The first lines of a set are those

@legend string   0 "Partition=  1 Excit=  1 near/far= 1"
#legend string   0 "Lab energy =  737.0000"
@s0 linestyle   1
#  Theta       sigma       iT11        T20         T21         T22         Kyy for projectile
  0.1000E-01   1.000    
  0.1000E-01   1.000    
  0.2000E-01   1.001    
  0.3000E-01   1.001    

This goes on and on until

   19.98      0.1659    
   19.99      0.1654    
   20.00      0.1649    
 END

The END text denotes the end of the set. Then, the new set begins

@legend string   1 "Partition=  1 Excit=  2 near/far= 1"
#legend string   0 "Lab energy =  737.0000"
@s1 linestyle   2
#  Theta       sigma       iT11        T20         T21         T22         Kyy for projectile
   0.000       26.40    
  0.1000E-01   26.41    
  0.2000E-01   26.45    
  0.3000E-01   26.52 

And this until the end.

As the "headers" of each set are the same and the number of lines per set are also the same, I could easily extract the sets I want by simply selecting the rows of the matrix I want... But how could I import this text file into a matrix or anything?

Thanks

Upvotes: 0

Views: 101

Answers (1)

Kostya
Kostya

Reputation: 1572

You can use something like this

fid = fopen( 'file1.txt' );
tline = fgetl(fid);
while ischar(tline)   
   disp(tline) %your parsing code here
   tline = fgetl(fid);
end
fclose( fid );

Upvotes: 1

Related Questions