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.

 採用された回答

per isakson
per isakson 2016 年 2 月 17 日

0 投票

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 件のコメント

Mark van Veenendaal
Mark van Veenendaal 2016 年 2 月 17 日
編集済み: per isakson 2016 年 2 月 18 日
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 2016 年 2 月 17 日
Just use textscan.
Mark van Veenendaal
Mark van Veenendaal 2016 年 2 月 17 日
Stephen, could you elaborate a little bit more please?
Mark.
per isakson
per isakson 2016 年 2 月 18 日
編集済み: per isakson 2016 年 2 月 18 日
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 ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by