how to read each text file using stread command
2 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
采纳的回答
Roger J
2020-8-1
编辑:Roger J
2020-8-1
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.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!