how to read each text file using stread command

3 views (last 30 days)
hello dear
I want to read text file of ground motion displacemet data (44 files) but only show the last file GM44
clear all; close all; clc;
Sdir='C:\users\USER\Desktop\MCE'; %C:\users\USER\Desktop\MCE
Model='SMRF10';
Odir = strcat(Sdir,'\',Model,'\'); %C:\users\USER\Desktop\MCE\SMRF10
for X =[1:44]
if X<10
Tag_GM =strcat('GM0', int2str(X))
else
Tag_GM =strcat('GM', int2str(X))
end
fid=fopen(strcat(Odir,Tag_GM,'\',Model,'-',Tag_GM,'-StoryDispl.out'),'r'); %C:\users\USER\Desktop\MCE\SMRF10\PushOver_StoryDispl
StoryDisp=fscanf(fid,'%c',[12,inf]);
fclose(fid);
SDisp = strread(StoryDisp);

Accepted Answer

Roger J
Roger J on 1 Aug 2020
Edited: Roger J on 1 Aug 2020
Aman,
I think that you want to end the script with one large matrix that has data from all files. And you want to print only the data from the last file.
If so, an easy way is to use a temporary matrix for reading the current file inside of the for loop, and use another matrix to accumulate all of the data. The nice thing is after the for loop finishes, you will have the big matrix that you wanted, but also the temporary matrix still has the data from the last file.
See the following changes to your code:
clear all; close all; clc;
Sdir='C:\users\USER\Desktop\MCE'; %C:\users\USER\Desktop\MCE
Model='SMRF10';
Odir = strcat(Sdir,'\',Model,'\'); %C:\users\USER\Desktop\MCE\SMRF10
accumulatedStoryDisp = []; %%%% this will hold all data from all files
for X =[1:44]
if X<10
Tag_GM =strcat('GM0', int2str(X))
else
Tag_GM =strcat('GM', int2str(X))
end %%%% end of the if/else
fid=fopen(strcat(Odir,Tag_GM,'\',Model,'-',Tag_GM,'-StoryDispl.out'),'r'); %C:\users\USER\Desktop\MCE\SMRF10\PushOver_StoryDispl
StoryDisp=fscanf(fid,'%c',[12,inf]);
accumulatedStoryDisp = [accumulatedStoryDisp ; StoryDisp]; %%%% add the current file's data to the accumulator
fclose(fid);
end %%%% end of for loop
% now you have all of the accumulated data in one very tall matrix "accumulatedStoryDisp"
% and the last file is also still available in the "StoryDisp" matrix
SDisp = strread(StoryDisp);
Let me know if it works for you, and mark it as the answer if it helps.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!