how to use sprintf inside a loop

4 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 2 Jul 2019
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 Comments
Samaneh Arzpeima
Samaneh Arzpeima on 2 Jul 2019
Star Strider thank you very much for the fast help
the first solution was what I was looking for
Star Strider
Star Strider on 2 Jul 2019
As always, my pleasure!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!