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!