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.

 Akzeptierte Antwort

per isakson
per isakson am 17 Feb. 2016

0 Stimmen

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 Kommentare

Mark van Veenendaal
Mark van Veenendaal am 17 Feb. 2016
Bearbeitet: per isakson am 18 Feb. 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 am 17 Feb. 2016
Just use textscan.
Stephen, could you elaborate a little bit more please?
Mark.
per isakson
per isakson am 18 Feb. 2016
Bearbeitet: per isakson am 18 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by