looking for specific data in a text file

Hi, I need to extract information out of a total of 150 text files which record variables in different locations for each text file, For example:
#--------------------------- CI - Effective/Macro Compliance Matrix
0.1910E-01 -0.7317E-02 -0.7317E-02
-0.7317E-02 0.3140E-01 -0.1456E-01
-0.7317E-02 -0.1456E-01 0.3140E-01
0.9510E-01
0.1051E+00
0.1051E+00
Effective Engineering Moduli
E11S= 0.5235E+02
N12S= 0.3830
E22S= 0.3185E+02
N23S= 0.4637
E33S= 0.3185E+02
G23S= 0.1051E+02
G13S= 0.9514E+01
G12S= 0.9514E+01
Effective Thermal Expansion Coefficients
0.0000E+00 0.0000E+00 0.0000E+00
Is there a fast way of getting E22S and G23S for all 150 text files and store these values in an array. The issue here is that each text file stores this info in different lines but in the same order. Thank you for your help!!
Regards,
Ernesto

 채택된 답변

Cedric
Cedric 2013년 8월 5일
편집: Cedric 2013년 8월 5일

0 개 추천

Something around the following solution would work well:
n = 150 ;
e22s = zeros(n,1) ;
g23s = zeros(n,1) ;
for k = 1 : n
fName = sprintf('myFile_%d.txt', k) ;
buffer = fileread(fName) ;
e22s(k) = str2double(regexp(buffer, '(?<=E22S=\s+)\S+', 'match')) ;
g23s(k) = str2double(regexp(buffer, '(?<=G23S=\s+)\S+', 'match')) ;
end

댓글 수: 2

Ernesto
Ernesto 2013년 8월 5일
Thank you so much! it works like a charm
Cedric
Cedric 2013년 8월 5일
Great, let me know if you need an explanation about the regexp pattern.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 5일

0 개 추천

fid = fopen('yourfilename.txt');
line1 = fgetl(fid);
res={line1};
while ischar(line1)
line1 = fgetl(fid);
res{end+1} =line1
end
fclose(fid);
res(end)=[]
idx=find(cellfun(@(x) ~isempty(regexp(x,'(E22S=)+','match')),res))
out=res{idx}

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

질문:

2013년 8월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by