How to save multiple MATLAB figures automatically in program?

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일
편집: BM 2018년 10월 16일
I am using 'savefig', but I get the error message 'Argument must be a .fig file'. My code is currently:
savefig(1,filename);
savefig(2,filename);
savefig(3,filename);
where there are 3 figures produced, and filename is a string defined via 'strcat()' in the script. This definition of the filename is necessary, as the filename is dynamical. My other attempts at using savefig have had issues as well. Also, I 'cd' to the correct directory I want to save the files in at the top of my script, so there should be no issue here with the location. Any suggestions from here?
Does filename end with the '.fig' extension?
filename is a string defined via 'strcat()' in the script
Please post the code instead of describing, what it should do. Obviously there is an error in the code, so we must see it to find the problem.
BM
BM 2018년 10월 16일
편집: BM 2018년 10월 16일
I cannot post the code. May I ask if one can give a general example of how to save MATLAB figures via a dynamical filename that changes with changing values as the program passes through iterations? This is what I am after. Thus, the filename would need to change as certain variables in the program would change. I am easily doing this with pdf and jpeg, but nothing seems to let me do this with MATLAB figures. Also, there would be 3 figures produced for each iteration. I need to save all three.
OCDER
OCDER 2018년 10월 16일
편집: OCDER 2018년 10월 16일
FigH = 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
BM
BM 2018년 10월 16일
편집: BM 2018년 10월 16일
Hi Steven,
No, the filename does not end with the 'fig' extension. According to (https://uk.mathworks.com/help/matlab/ref/savefig.html), I don't need one as the instructions seem to suggest that using the format: savefig(H,filename) saves figures identified by the graphics array H as filename.fig. What am I missing? My filename works with the initial code I have written in my initial question just fine. I am now trying to save the figures as fig files instead of jpg.
Hi OCDER,
Your code seems to work, but only will save 1 out of the 3 figures produced each iteration. I modified it slightly to save all three figures! Works though!
Thanks again!
You're welcome!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Printing and Saving에 대해 자세히 알아보기

태그

질문:

BM
2018년 10월 16일

댓글:

2018년 10월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by