Export figure using print with variable as the file name

19 ビュー (過去 30 日間)
Miraboreasu
Miraboreasu 2023 年 1 月 6 日
コメント済み: Miraboreasu 2023 年 1 月 8 日
x1=1:1:1000;
check = rand(1000,35);
cmap = hsv(5);
for K = 1 : 5
thiscolor = cmap(K,:);
figure
for J = K:5:35
plot(x1,check(:,J), '-o','color', thiscolor);
grid on
grid minor
hold on
end
set(gca,'fontweight','bold','FontSize',25)
print(gcf,'E%d.png',K,'-dpng','-r600'); %Here is the problem
end
How to export the figure with K as the variable

採用された回答

Adam Danz
Adam Danz 2023 年 1 月 6 日
編集済み: Adam Danz 2023 年 1 月 7 日
Use exportgraphics (or another function export function, if needed) and define the filename using sprintf.
Example:
fig = figure();
% do your plotting here
filename = sprintf('E%d.png', k);
exportgraphics(fig,filename,'Resolution',600)
Alternatively,
filename = string(k);

その他の回答 (1 件)

Adam
Adam 2023 年 1 月 6 日
The problem with this code is that the print function is not being used correctly. The first argument to print should be the file name to save the plot to, but in this case it is the handle to the figure object that is being plotted. The file name should be specified as the second argument, using the '-f' option.
To fix the problem, you can change the line
print(gcf,'E%d.png',K,'-dpng','-r600');
TO
print('-f','E%d.png',K,'-dpng','-r600');
  1 件のコメント
Adam Danz
Adam Danz 2023 年 1 月 7 日
The leading figure handle argument is supported: print(fig,___)
The K is the problem since it is not part of the filename.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by