From: Jeff Rzepiela on
I want to print a Matlab figure to a fixed size jpeg file (480x400 pixels). I have tried:

set(fig,'PaperUnits','inches','PaperPosition',[0 0 4.8 4])
print(fig,'-djpeg100',(fullfile(pathName,[fileName '.jpg'])),'-r100');

That creates the correct file size, but the image is a cropped version of the Matlab figure (which is 560x420). What I really want is to scale the figure down to the correct size and print to jpeg so that the image contains the whole figure.

I tried adjusting height and width in 'Position' property, but that had no effect on the jpeg file.

Thanks for any suggestions,

- Jeff
From: Matthew Whitaker on
Jeff,

You can get a frame of the figure

Here a quick function:
function setSizeAndSaveFigure(f,jpegSize,jpegFile)
%setSizeAndSaveFigure set the figure size and then saves to a JPEG

origFigurePos = getpixelposition(f);
setpixelposition(f,[origFigurePos(1:2),jpegSize]);

F = getFrame(f);

im = frame2im(F);

imwrite(im,jpegFile,'jpg');

setpixelposition(f,origFigurePos);

drawnow;
end

Example:
plot(sin(0:0.1:2*pi))
setSizeAndSaveFigure(gcf,[400,400],'SavedImage.jpg')


Hope this helps
Matt W


"Jeff Rzepiela" <jeffrz(a)mindspring.com> wrote in message <i3fd5o$osl$1(a)fred.mathworks.com>...
> I want to print a Matlab figure to a fixed size jpeg file (480x400 pixels). I have tried:
>
> set(fig,'PaperUnits','inches','PaperPosition',[0 0 4.8 4])
> print(fig,'-djpeg100',(fullfile(pathName,[fileName '.jpg'])),'-r100');
>
> That creates the correct file size, but the image is a cropped version of the Matlab figure (which is 560x420). What I really want is to scale the figure down to the correct size and print to jpeg so that the image contains the whole figure.
>
> I tried adjusting height and width in 'Position' property, but that had no effect on the jpeg file.
>
> Thanks for any suggestions,
>
> - Jeff