MATLAB: How to get distict values from many text files (please help)

text filetext;textscan

i have many text file like i shared below and each of them has same type. i want to get some values from them and put those values into the calculation. For example i need the value of Area, , xbar,ybar,IXXw and Iyyw . how can i get those values from all files and find mean value of the
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 Xp(X) = -0.55201395809680 Xp(Y) = 0.83383486978316 Yp(X) = 0.83383486978316 Yp(Y) = 0.55201395809680
Principal Moments of Inertia Ixxp = 31806658.8454180 Iyyp = 8513379.53177490
Circle Size Center XC = 50.0 YC = -100.0 Diameter = 269.258240356730

Best Answer

The following approach is based on regular expressions:
files = {'data_01.txt', 'data_02.txt'} ;
nFiles = numel(files) ;
area = zeros(nFiles, 1) ; % Prealloc.
xbar = zeros(nFiles, 1) ;
ybar = zeros(nFiles, 1) ;
ixxw = zeros(nFiles, 1) ;
iyyw = zeros(nFiles, 1) ;
for fId = 1 : nFiles
content = fileread(files{fId}) ;
area(fId) = str2double(regexp(content, '(?<=Area \= )[\-\d\.]+', ...
'match', 'once')) ;
xbar(fId) = str2double(regexp(content, '(?<=Xbar \= )[\-\d\.]+', ...
'match', 'once')) ;
ybar(fId) = str2double(regexp(content, '(?<=Ybar\= )[\-\d\.]+', ...
'match', 'once')) ;
ixxw(fId) = str2double(regexp(content, '(?<=Ixxw \= )[\-\d\.]+', ...
'match', 'once')) ;
iyyw(fId) = str2double(regexp(content, '(?<=Iyyz \= )[\-\d\.]+', ...
'match', 'once')) ;
end
EDIT : here is a little more elaborate version
files = {'data_01.txt', 'data_02.txt'} ;
items = {'Area', 'Xbar', 'Ybar', 'Ixxw', 'Iyyw'} ;
nFiles = numel(files) ;
nItems = numel(items) ;
data = struct('fId', {}) ;
for fId = 1 : nFiles
content = fileread(files{fId}) ;
data(fId).fId = fId ;
for iId = 1 : nItems
data(fId).(items{iId}) = str2double(regexp(content, ...
sprintf('(?<=%s \\= )[\\-\\d\\.]+', items{iId}), ...
'match', 'once')) ;
end
end
which generates a struct array, e.g., after running this code using two fake files built for the example:
>> data
data =
1x2 struct array with fields:
fId
Area
Xbar
Ybar
Ixxw
Iyyw
>> data(1)
ans =
fId: 1
Area: 11875
Xbar: -9.2105
Ybar: -89.4737
Ixxw: 1.1068e+08
Iyyw: 2.5716e+07
>> data(2)
ans =
fId: 2
Area: 11876
Xbar: -9.4105
Ybar: -80.4737
Ixxw: 2.1068e+08
Iyyw: 1.5716e+07
>> mean([data.Xbar])
ans =
-9.3105