how to use sprintf inside a loop

4 ビュー (過去 30 日間)
Samaneh Arzpeima
Samaneh Arzpeima 2019 年 7 月 2 日
コメント済み: Star Strider 2019 年 7 月 2 日
Hello
I have multiple mat file, I need to plot specide colomns of each on the same figure
I don't now how to refer file names using sprintf
please tell me what is the correct format to read file name
I have
momentrate_S1_SS
momentrate_S1_rev
momentrate_S1_nor
momentrate_S2_SS
momentrate_S2_rev
momentrate_S2_nor
momentrate_S3_SS
momentrate_S3_rev
momentrate_S3_nor
momentrate_S4_SS
momentrate_S4_rev
momentrate_S4_nor
.
.
.
.
I want to plot "momentrate_S*_SS" vs "momentrate_S*_rev" vs "momentrate_S*_nor"
*is the same number
the following gives error
hold on
for filetype = ['SS' 'rev' 'nor']
number=1:10
filedata3 = load(fullfile('D:\Jobs_2019\self\verhor\round3\hikaku_plot', sprintf('momentrate_S%d_%3s.mat', number,filetype)));
plot(filedata3.momentrate(:,1), filedata3.momentrate(:,2));
end
thank you

採用された回答

Star Strider
Star Strider 2019 年 7 月 2 日
Try this:
filetype = {'SS' 'rev' 'nor'};
figure
hold all
for k1 = 1:numel(filetype)
for k2 = 1:10
filedata3 = load(fullfile('D:\Jobs_2019\self\verhor\round3\hikaku_plot', sprintf('momentrate_S%d_%3s.mat', k2, filetype{k1})));
plot(filedata3.momentrate(:,1), filedata3.momentrate(:,2));
end
end
hold off
I obviously cannot test all of it, however I did test the sprintf call, and it works correctly. I added the hold all call to plot all the file data on the same axes.
If you want 30 different plots instead, do this:
filetype = {'SS' 'rev' 'nor'};
for k1 = 1:numel(filetype)
for k2 = 1:10
filedata3 = load(fullfile('D:\Jobs_2019\self\verhor\round3\hikaku_plot', sprintf('momentrate_S%d_%3s.mat', k2, filetype{k1})));
figure
plot(filedata3.momentrate(:,1), filedata3.momentrate(:,2));
end
end
Experiment to get the result you want.
  2 件のコメント
Samaneh Arzpeima
Samaneh Arzpeima 2019 年 7 月 2 日
Star Strider thank you very much for the fast help
the first solution was what I was looking for
Star Strider
Star Strider 2019 年 7 月 2 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by