How to save multiple MATLAB figures automatically in program?

94 Ansichten (letzte 30 Tage)
BM
BM am 16 Okt. 2018
Kommentiert: OCDER am 16 Okt. 2018
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.

Akzeptierte Antwort

OCDER
OCDER am 16 Okt. 2018
Bearbeitet: OCDER am 16 Okt. 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 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by