how to get distict values from many text files (please help)
Mostra commenti meno recenti
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 Commento
per isakson
il 5 Set 2013
Modificato: per isakson
il 5 Set 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?
Risposta accettata
Più risposte (1)
Yusuf
il 6 Set 2013
0 voti
3 Commenti
Do you understand the simplest solution involving e.g.
area(fId) = str2double(regexp(content, '(?<=Area \= )[\-\d\.]+', ...
'match', 'once')) ;
If not, I can explain that to you (just ask). If you do understand it, then the only difference in the second solution is that instead of having as many arrays as items, we build a struct array whose fields are item names, and we build the regexp pattern using corresponding item names. So what you see in
data(fId).(items{iId}) = str2double(regexp(content, ...
sprintf('(?<=%s \\= )[\\-\\d\\.]+', items{iId}), ...
'match', 'once')) ;
is dynamic field names and a generation of the pattern using SPRINTF.
About dynamic field names, if s is a struct
>> s = struct() ;
you can access fields as follows
>> s.age = 65 ;
>> s.name = 'Jon' ;
As you can see, field names are hardcoded. Now if a field name must be defined (dynamically) as a string stored in some variable, we can use the following type of expression
>> fName = 'city' ;
>> s.(fName) = 'New York' ;
>> s
s =
age: 65
name: 'Jon'
city: 'New York'
Here you can see that the field name 'city' was stored in a char variable fName (it is its content) and could be used to index s. This is what we do in
data(fId).(items{iId}) = ...
where we use items{iId} as a field name. We also want to use items{iId} for building the regexp pattern and we do it with SPRINTF:
sprintf('(?<=%s \\= )[\\-\\d\\.]+', items{iId})
which is almost the same as in the simple example with the difference that, as \ is an escape character for SPRINTF, we have the escape it so it becomes a \ character in the output of SPRINTF.
Hope it helps; let me know if you still don't understand.
Yusuf
il 9 Set 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.
Categorie
Scopri di più su Text Data Preparation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!