How to save multiple MATLAB figures automatically in program?

90 views (last 30 days)
In my script, I may save the figures as jpg files via the code (which works):
% print(1,'-painters,'-djpeg',strcat(filename,'r','.jpg'))
I want to do the same thing, but saving my files as figs in MATLAB. That's it, but it has to work the same, which means the filename must be saved as well. This is variable with the iterations of the program.
I should also mention there are two more print commands in the original code that are identical except for the number and the colour.

Accepted Answer

OCDER
OCDER on 16 Oct 2018
Edited: OCDER on 16 Oct 2018
Note: You should save using the full file name, which includes the folder path AND file name. Otherwise, you cannot use your program if your current working directory is different.
FullFileName = fullfile(FilePath, FileName)
Here's an example.
%Example
FigH = figure; %If you have other figure handles, then specify
%this when saving figures. Note: you can store
%figure handles in a cell array or gobjects
%array, in case you need to loop through the saves.
%EX: FigH{1} = figure. FigH{2} = figure.
AxH = axes;
X = 1:100;
Y = sin(X);
FileDir = pwd; %Replace with your folder
for j = 1:10
LineH = plot(AxH, X, Y*j);
FilePre = sprintf('%s-%d', 'MyFig', j);
savefig(FigH, fullfile(FileDir, [FilePre '.fig']))
print(FigH, fullfile(FileDir, [FilePre '.png']), '-r300', '-dpng')
%Changing figure color
LineH.Color = 'r';
savefig(FigH, fullfile(FileDir, [FilePre 'red.fig']))
print(FigH, fullfile(FileDir, [FilePre 'red.png']), '-r300', '-dpng')
end
  9 Comments

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!