how to get some values from many text files and put them into the calculation ( please help)
이전 댓글 표시
hello,
I have some text files have same content such as the text i shared below. and i need value of i need the value of Area, xbar,ybar,IXXw and IYYw. how can i get them from,for example,5 text file which have name data1, data2, data3, data4, data5 and for example i want to calculate the mean value of areas?
============================================================
Information listing created by : T40118
Date : 29.08.2013 11:07:12
Current work part : FA00AAC41319/002
Node name : tofdsk03221
============================================================
Work Part FA00AAC41319/002 : 08/29/13 11:07
Information Units kg - mm
Perimeter = 765.13878188660
Area = 11875.0
First Moments
MY = -109375.0
MX = -1062500.0
Center of Mass
Xbar = -9.21052631578950
Ybar = -89.4736842105260
Moments of Inertia (Work)
Ixxw = 110677083.333330
Iyyw = 25716145.8333330
Moments of Inertia (Centroidal)
Ixx = 15611293.8596490
Iyy = 24708744.5175440
Moment of Inertia (Polar)
= 40320038.3771930
Product of Inertia (Work)
Pxyw = 20507812.50
Product of Inertia (Centroidal)
Pxy = 10721628.2894740
Radii of Gyration (Work)
Rgxw = 96.5410557151540
Rgyw = 46.5356871168630
Radii of Gyration (Centroidal)
Rgx = 36.2578994481410
Rgy = 45.6150893940230
Radii of Gyration (Polar)
= 58.2698176830530
Principal Axes
채택된 답변
추가 답변 (1개)
Walter Roberson
2013년 9월 11일
AveVariables = {'Area', 'xbar', 'ybar', 'IXXw', 'IYYw'}; %variables to average over
matchpats = cellfun( @(P) ['^' P '\s*=\s*(?<' P '>\S+)\s*$' ], AveVariables, 'Uniform', 0);
allvals = cell2struct( cell(length(AveVariables,1), AveVariables);
Files = dir('data*.txt'); %get files
for K = 1 : length(files)
thistext = fileread( files(K).name );
matchtokens = regexp( thistext, matchpats, 'tokens', 'lineanchors' );
allvals(K) = matchtokens;
end
Now allvals will be a struct array with the K'th element being a struct whose fields are named according to the variable names. With this version of the code, each field will contain a string. If all of the variable are numeric and you want them converted, you could use
allvals(K) = structfun( @str2double, matchtokens, 'Uniform', 0 );
카테고리
도움말 센터 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!