How to extract a number from .prn file

31 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by