MATLAB Answers

0

How to "Save As .jpg" a figure programmically?

Asked by Mikael Agopov on 20 Mar 2019
Latest activity Edited by Mikael Agopov on 26 Mar 2019
Accepted Answer by Jan
I've got a following problem: I need to automatically save figures as jpg files. This is easily done by hand
(open the File-menu, go to Save As, choose jpg image). But how to do this programmically?
Mikael

  1 Comment

Sign in to comment.

Products


Release

R2018b

1 Answer

Answer by Jan
on 20 Mar 2019
Edited by Jan
on 20 Mar 2019
 Accepted Answer

Either by
print(FigureHandle, 'C:\temp\File.jpg')
or
pause(0.02); % Magic update of java elements - don't ask me why...
F = getframe(FigureHandle);
[X, map] = frame2im(F);
imwrite(X, map, 'C:\temp\File.jpg');

  1 Comment

Thanks to Walter and Jan,
Solved the issue using print():
1) take the screencapture and save it to the object as a property
sc=print('-RGBImage');
set(obj,'ScreenCapt',sc);
...
2) later, when saving results, open up a dummy figure (not visible), imshow the screencapture on it and print the figure
into a file.
H=figure('Visible','off');
imshow(get(obj,'ScreenCapt'));
print(H,'screencapt.png','-dpng');

Sign in to comment.