Extracting numerical results from a text file at different positions

조회 수: 1 (최근 30일)
GS76
GS76 2019년 3월 10일
댓글: GS76 2019년 3월 11일
I have a number of output files to process, written out from Finite Element Analysis software, from which I need to extract data.
I need to extract data under 2 different "headings" from each of the files. However, each of the files has these headings at different rows. I have attached a "zipped" file with 2 files indicating this problem.
The following screenshots show the "headings" highlighted and the numerical data to be extracted, shown inside a "box":
First Heading
H0.PNG
Second Heading
H2.PNG
I have tried a number of options in Matlab, but I am overwhelmed as I am new to Matlab and programming.
Any assistance and guidance, would be much appreciated.

채택된 답변

GT
GT 2019년 3월 11일
good catch, my bad:) it was late at night. This should work. In either case if you are learning MATLAB, regexp are very powerful. They exist in many languages, it is worth understanding them:)
a = fileread('0295_PhD_AB~Analysis 1.txt');
% find ( H A R M O N I C = \d )
[b,c] = regexp(a,'( H A R M O N I C = \d )','tokens');
result = table();
% find the 2nd and the 6th number in the table
for i = 1:length(c)
aux = (a(c(i)+586:c(i)+700));
d = regexp(aux,'([0-9.E-+]*)','tokens');
result = [result;table(repmat(b{i},2,1),[d{2};d{6}])];
end
result.Var2 = str2double(result.Var2);
  댓글 수: 1
GS76
GS76 2019년 3월 11일
Thank you GT!
This worked out excellently.
I will continue learning and practising "regexp". Thank you for all your help and tips.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

GT
GT 2019년 3월 10일
There is probably an easier way to do this... but this should help. (I am using R2018b).
a = fileread('0295_PhD_AB~Analysis 1.txt');
% find ( H A R M O N I C = \d )
[b,c] = regexp(a,'( H A R M O N I C = \d )','tokens');
result = table();
% find
for i = 1:length(c)
aux = (a(c(i)+586:c(i)+700));
d = regexp(aux,'([0-9.E-]*)','tokens');
result = [result;table(repmat(b{i},2,1),[d{2};d{7}])];
end
result
  댓글 수: 2
GT
GT 2019년 3월 10일
You can always convert the last column into doubles...
result.Var2 = str2double(result.Var2);
GS76
GS76 2019년 3월 11일
Hi GT,
Thank you very much for this help. This is exactly what I was hoping to achieve.
However, I found the following error whereby the code is extracting the last value from the second column and not the first, when extracting for "HARMONIC = 2". Please see the attached screenshot:
Error.PNG
Could you assist me to correct this small problem in the code.
Thank you again for your assistance.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by