I have a large data file and I want to extract specific data from this file. However, I want to program the system in such a way that I can input the same sort of data file into the program and it will still give the same outcome. So for example, there is a string called
tline =' : W A V E P E R I O D = 3.6000E+01 : ';
within the data file. This string occurs on different locations for different data files. What I want to extract from the data file is 3.6000E+01. What I have got so far (but does not work) is:
A = sscanf(tline,' : W A V E P E R I O D = %1.4e : ');
Regards, Mark.

 Réponse acceptée

per isakson
per isakson le 17 Fév 2016
Try this
str = fileread( 'lorem.txt' );
cac = regexp( str, '(?<=W A V E P E R I O D =)\s*[\d\.\E\+\-]+', 'match','once' )
it outputs
cac =
3.6000E+01
where lorem.txt is attached

4 commentaires

Mark van Veenendaal
Mark van Veenendaal le 17 Fév 2016
Modifié(e) : per isakson le 18 Fév 2016
Thanks Per,
I have managed to obtain it. Would it also be possible to obtain a matrix in this manner? This would be the string.
ADDED MASS MATRIX
-----------------
1 2 3 4 5 6
1 1.0000E-02 0.0000E+00 1.0000E-02 0.0000E+00 1.0000E-02 0.0000E+00
2 0.0000E+00 1.0000E-01 0.0000E+00 -1.0000E-03 0.0000E+00 -1.0000E-03
3 1.0000E-02 0.0000E+00 1.0000E+00 0.0000E+00 1.0000E-01 0.0000E+00
4 0.0000E+00 -1.0000E-02 0.0000E+00 1.0000E-03 0.0000E+00 1.0000E-03
5 1.0000E-02 0.0000E+00 1.0000E-01 0.0000E+00 1.0000E-01 0.0000E+00
6 0.0000E+00 -1.0000E-03 0.0000E+00 1.0000E-03 0.0000E+00 1.0000E-02
Where I'm only interested in the following numbers:
1.0000E-02 0.0000E+00 1.0000E-02 0.0000E+00 1.0000E-02 0.0000E+00
0.0000E+00 1.0000E-01 0.0000E+00 -1.0000E-03 0.0000E+00 -1.0000E-03
1.0000E-02 0.0000E+00 1.0000E+00 0.0000E+00 1.0000E-01 0.0000E+00
0.0000E+00 -1.0000E-02 0.0000E+00 1.0000E-03 0.0000E+00 1.0000E-03
1.0000E-02 0.0000E+00 1.0000E-01 0.0000E+00 1.0000E-01 0.0000E+00
0.0000E+00 -1.0000E-03 0.0000E+00 1.0000E-03 0.0000E+00 1.0000E-02
Regards, Mark.
Stephen23
Stephen23 le 17 Fév 2016
Just use textscan.
Stephen, could you elaborate a little bit more please?
Mark.
per isakson
per isakson le 18 Fév 2016
Modifié(e) : per isakson le 18 Fév 2016
Short answer: NO!
To try to extract that numerical block with regular expression would be a severe waste of time.
If the text file looks "exactly" as in your question this reads it
fid = fopen( 'AddedMass.txt' );
cac = textscan( fid, '%*d%f%f%f%f%f%f', 'Headerlines',4, 'Collectoutput',true )
[~] = fclose( fid );
outputs
cac =
[6x6 double]
However, if the string is embedded somewhere in a text file it becomes a bit tricky.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by