How to extract a number from .prn file

21 ビュー (過去 30 日間)
Tsuwei Tan
Tsuwei Tan 2018 年 6 月 25 日
回答済み: Walter Roberson 2018 年 6 月 26 日
May I ask how to extract certain number from .prn file? For instance, I have a file called HLA31_Shru00070.prn which is contained in the .zip file how do I extract a particular number which is mode 2 group speed its value is 1471.158022 as attached picture?
I have numerous .prn files that I have to extract its corresponding group speed, such as HLA31_Shru00055.prn HLA31_Shru00056.prn ... HLA31_Shru00070.prn HLA31_Shru00071.prn ...
Understood that I will need a for loop to do it, may I ask how to extract numerous files simultaneously? Do I need to rename the prn files so Matlab can read it?
Thank you!!
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 6 月 25 日
Is the data always at the same line number in the file?
"Do I need to rename the prn files so Matlab can read it?"
No, that will not be necessary, but exactly how easy it is will depend upon the answer the question about line number.
Tsuwei Tan
Tsuwei Tan 2018 年 6 月 25 日
Dear Walter, thank you for your quick response! It seems to me that they are in the same line when I use Microsoft Word to read those files; they are both at the last page and the same line. I need to extract the 2nd element of group speed column, thank you!

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2018 年 6 月 26 日
filename = 'LHA31_Shru_00070.prn';
S = fileread(filename);
GStr = regexprep(S, {'^.*GROUP SPEED.*?$', '^.*?copied.*'}, {'', ''}, 'lineanchors');
Now GStr should contain just the lines of the GROUP SPEED table. You could then
GSdata = cell2mat( textscan(GStr, '%f%f%f%f%f', 'CollectOutput', true) );
and GSdata would be a numeric array that you could extract any desired parts from.
The process would be easier still if the exact line number of the GROUP SPEED header was known.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by