Data extraction with undefined file length

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.

 采纳的回答

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 个评论

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.
Stephen, could you elaborate a little bit more please?
Mark.
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.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by