In a MATLAB function I am writing, I am generating a figure. The figure is displayed when the function is executed. I need to save the figure as a JPEG image. To do that, I could do File->Save As in the figure window that displays the figure. But I'd like to automate this. I've tried to do that using the saveas() function. The problem is that the resulting image is undesirable. Here are the images for a demo problem to show you what I mean:
JPEG image saved manually using File->Save As in the MATLAB figure window:
JPEG Image saved using saveas() function (notice that the plots aren't as nice and the titles overlap):
Here is the MATLAB function in which I generate the figure and save the figure as a JPEG using saveas():
function JpgSaveIssueDemo( )
figure( 1 );
t = 0:0.1:8;
subplot( 2, 2, 1 );
plot( t, sin(t) );
title( 'Plot 1 of Example to Demonstrate JPG Save Issue', 'FontSize', 18 );
subplot( 2, 2, 2 );
plot( t, sin(t) );
title( 'Plot 2 of Example to Demonstrate JPG Save Issue', 'FontSize', 18 );
subplot( 2, 2, 3 );
plot( t, sin(t) );
title( 'Plot 3 of Example to Demonstrate JPG Save Issue', 'FontSize', 18 );
subplot( 2, 2, 4 );
plot( t, sin(t) );
title( 'Plot 4 of Example to Demonstrate JPG Save Issue', 'FontSize', 18 );
saveas( gcf, 'DemoPlot', 'jpg' );
end
The figure that is displayed when JpgSaveIssueDemo() is executed isn't maximized. So, I thought that if I could maximize it using function call/s in JpgSaveIssueDemo() before saveas() is executed, then the JPEG image saved would come out well.
So, I used this code before the saveas() line in JpgSaveIssueDemo() to maximize the figure:
drawnow;
jFrame = get(handle(gcf),'JavaFrame');
jFrame.setMaximized(true);
Then, the figure that is displayed is maximized. However, the result is the same: the JPEG image still comes out undesirably.
What can be done for this?