# how to get some values from many text files and put them into the calculation ( please help)

2 views (last 30 days)
Yusuf on 11 Sep 2013
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
Rgxw = 96.5410557151540
Rgyw = 46.5356871168630
Rgx = 36.2578994481410
Rgy = 45.6150893940230
= 58.2698176830530
Principal Axes

Matt Kindig on 11 Sep 2013
Edited: Matt Kindig on 11 Sep 2013
One approach using regular expressions:
%variables to average over
AveVariables = {'Area', 'xbar', 'ybar', 'IXXw', 'IYYw'};
AveVariables = lower(AveVariables); %remove case
Files = dir('data*.txt'); %get all dataXX files
nFiles = length(Files);
AllData = NaN( nFiles, length(AveVariables)); %variables for each file
for k = 1:nFiles;
File = Files(k).name;
pat = '\(*(?<var>[A-Z]+)\)*\s+\=\s+(?<val>[0-9\.\-]+)'; %define pattern
%search pattern is:
%[series of letters, possibly surrounded by parens] = [signed decimal number]
variables = regexpi(str, pat, 'names'); %match pattern
values = {variables.val};
vars = lower({variables.var});
[tf,loc]=ismember( AveVariables, vars); %compare to target variables
values = str2double(values(loc(tf))); %convert to numeric
end
AveData=nanmean(AllData,1); %average over files
Yusuf on 13 Sep 2013
thank you for your answer, it works well but just one more question which is that how i can use those values, for example, i want to define n : Xbar(average)/2 , when i try to do this,n = 'xbar'/2, i got matrix like this
60.0000 49.0000 48.5000 57.0000
but i just want to use avarage value of Xbar and divide it by two, how i can do this ?

Walter Roberson on 11 Sep 2013
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)
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 );