how to get distict values from many text files (please help)

1 view (last 30 days)
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
  1 Comment
per isakson
per isakson on 5 Sep 2013
Edited: per isakson on 5 Sep 2013
Is there a varying number of "=" per line or is it a problem with line breaks?
What is the typical size of the file? Is performance an issue?

Sign in to comment.

Accepted Answer

Cedric
Cedric on 5 Sep 2013
Edited: Cedric on 5 Sep 2013
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

More Answers (1)

Yusuf
Yusuf on 6 Sep 2013
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')) ; i just dont understand this part can you explain little bit more?
  3 Comments
Yusuf
Yusuf on 9 Sep 2013
thank you for detailed explanation but i am beginner on matlab and i used your code but i got this error;
Error using fileread (line 27) Could not open file data_01.txt. No such file or directory.
so just to make sure that the name of text files you used here are data_01 and data_02, arent they?
and do i need to change any variable here? for example "n" depends on the number of my files in those codes? nFiles = numel(files) for fId = 1 : nFiles
Cedric
Cedric on 9 Sep 2013
Edited: Cedric on 9 Sep 2013
The only thing that you should do is to define the cell array files with your own file names instead of
files = {'data_01.txt', 'data_02.txt'} ;
I chose these file names for the example actually. Other than that, there is nothing to do except to understand how to use the data container in which numbers are stored. The first solution is the simplest on that matter, as it involves basic numeric arrays.
If you had hundreds of files in a folder and didn't want to list them all by hand for defining the cell array files, you could use a call to DIR to retrieve a directory listing. For example, if you had this MATLAB script in a folder C:\Yusuf and all data files in C:\Yusuf\data with the extension .txt, you could replace the line
files = {'data_01.txt', 'data_02.txt'} ;
by
files = dir(fullfile('data', '*.txt')) ;
and then the line
content = fileread(files{fId}) ;
by
content = fileread(fullfile('data', files(fId).name)) ;
This way, running the script would provide you with a result based on all .txt files in C:\Yusuf\data.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!