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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by