How to save multiple MATLAB figures automatically in program?

조회 수: 94 (최근 30일)
BM
BM 2018년 10월 16일
댓글: OCDER 2018년 10월 16일
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.

채택된 답변

OCDER
OCDER 2018년 10월 16일
편집: OCDER 2018년 10월 16일
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
BM
BM 2018년 10월 16일
Thanks again!
OCDER
OCDER 2018년 10월 16일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by