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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!