필터 지우기
필터 지우기

How to extract a number from .prn file

조회 수: 31 (최근 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 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