Read a specific number in a text file containing a mixture of strings and numbers

2 views (last 30 days)
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

Accepted Answer

Geoff Hayes
Geoff Hayes on 10 Oct 2015
Edited: Geoff Hayes on 10 Oct 2015
Mamali - if you can assume that all text files are the same and so follow the same pattern which is that you are interested in the first number of the line that follows the one that contains the word Drifts then you can use the following to grab that value.
numFiles = 1998;
myData = zeros(numFiles,1);
for k=1:numFiles
% create the file name
filename = sprintf('text%d.txt',k);
% try to open the file
fid = fopen(filename,'r');
if fid > 0
% loop over each line until the end of file (eof) is reached
while ~feof(fid)
% grab the next line in the file
lineTxt = fgetl(fid);
% look for the text Drifts which indicates that the next line
% is where we want to grab the number from
if strfind(lineTxt,'Drifts')
% get the next line
lineTxt = fgetl(fid);
% split the line of text into a cell array using the space
% character as the delimiter
cellArray = strsplit(lineTxt,' ');
% grab the second element of the array
numOfInterest = str2double(cellArray{2});
% do something with this number
myData(k) = numOfInterest;
% exit from file
break;
end
end
% close the file
fclose(fid);
end
end
Try the above and see what happens!
  3 Comments
Geoff Hayes
Geoff Hayes on 10 Oct 2015
Mamali - try creating an array for each of those values read from the file. See the edited code above. Note that I'm not clear why results are strings especially after the str2double conversion.
Antonio
Antonio on 10 Oct 2015
Geoff - your code works perfectly. I appreciate your kind assistance. :) About the results, they became strings when I copied them from MATLAB to excel! I solved this issue anyway. Thanks a lot ...

Sign in to comment.

More Answers (1)

per isakson
per isakson on 10 Oct 2015
Edited: per isakson on 10 Oct 2015
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.

Community Treasure Hunt

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

Start Hunting!