How to save multiple figures created by bar() in a loop with different name?

2 次查看(过去 30 天)
I have seen many posts regarding this but none of them worked for me. I have the following code in which I want to save created figures in two formats .fig and .png. But every time the compiler says "Unable to open file "E:\figs\RX 1: Iteration= 100.fig" for output."
fpath = 'E:\figs\';
% Plotting the results
c = X; % Something in c
for i = 1:size(input,1)
figure(i);
title(sprintf('RX %d: Iteration=%d, zeta=%3.2f, n=%d, p=%d',i,iteration_count_par,zeta_par,n_par,p_par));
ylabel('Ratio (%)');
hold on;
y = Y; % Something in y
bar(c,y);
legend('Accuracy','False Positive Ratio','False Negative Ratio');
fname = sprintf('E:\figs\RX %d: Iteration=%d',i,iteration_count_par);
saveas(gca,fullname(fpath,fname),'fig');
saveas(gca,fullname(fpath,fname),'png');
hold off;
end

回答(1 个)

KL
KL 2017-8-30
编辑:KL 2017-8-30
for iCount = 1:3
h = figure;
bar(rand(iCount))
fname_fig = ['figure_' num2str(iCount) '_in_fig_format.fig'];
fname_png = ['figure_' num2str(iCount) '_in_png_format.png'];
saveas(h,fullfile(pwd,fname_fig));
saveas(h,fullfile(pwd,fname_png));
end

Community Treasure Hunt

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

Start Hunting!

Translated by