how to use sprintf inside a loop

4 次查看(过去 30 天)
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by