MATLAB Answers

How to extract a number from .prn file

35 views (last 30 days)
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!!


Walter Roberson
Walter Roberson on 25 Jun 2018
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 on 25 Jun 2018
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!

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Jun 2018
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.


Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!