Read a specific number in a text file containing a mixture of strings and numbers
이전 댓글 표시
Hi there,
I need to read "a specific number" (highlighted below) in 1998 different text files (text1.txt, text2.txt, ..., text1998.txt) simultaneously and print that numbers in another file (preferably, an excel file). All the files are a mixture of strings and numbers and look like as follows:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Structure: Biaxial House model with 1 story.
Maximum responses---------------------------
Displacements: X Y R Story--1: 1.267323e+000 1.251265e-016 1.574376e-011
Maximum Inter-story responses---------------------------
Inter-story Drifts: dX dY dR Story--1: 1.267323e+000 1.251265e-016 1.574376e-011
Maximum story forces---------------------------
Total force at story: Fx Fy Mr Story--1: 6.152071e+003 1.364242e-012 4.009894e-003
Maximum story Accelerations---------------------------
Global Accel at story Centroid: Ax Ay Ar Story--1: 3.860713e+001 9.869805e-017 3.364166e-010
The average acceleration is calculated using a 0.01sec time interval Note: If the specified time interval is smaller than the time interval of input excitation, the excitation time interval is used to calculate the average Accel. Maximum story Average Accelerations---------------------------
Global Accel at story Centroids:(average) Ax Ay Ar Story--1: 8.247387e-001 9.865127e-017 2.810327e-010
Building Damage index calculated based on Hysteretic elements: 8.239350e-001
----------------------------------------------------------------------------------------------------------------------------------------------------------------
I really don't know how to do that (I know how to read a simple excel file, or how to read a *.txt file that only contains numbers, but really got into trouble with reading that specific number in a mixed file, and also don't know how to use a 'for' loop that reads all the 1998 files for me!). So, could any of you do me a favor and help me with this please (a sample file is also attached below. Please note that the suffix is *.gen but I use notepad to read these files)?!
Thanks a lot
채택된 답변
추가 답변 (1개)
per isakson
2015년 10월 10일
편집: per isakson
2015년 10월 10일
An alternative using regular expressions to parse one file
tic
str = fileread('MM1E1I1.txt');
xpr_num = [
'\d+' ... one or more digits
'(\.\d+)?' ... zero or one group of one dot and one or more digits
'([eE][\+-]\d{3})?' ... zero or one group of the letter E, "+" or "-", and three digits
];
cac = regexp( str ...
, ['(?<=Inter-story Drifts: +dX +dY +dR\r?\nStory--1: +)',xpr_num] ...
, 'match', 'once' );
toc
and
>> str2double( cac )
ans =
1.2673
With R2013a,64bit on an old vanilla desktop: Elapsed time is 0.002555 seconds.
카테고리
도움말 센터 및 File Exchange에서 Variables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!