Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
saving current figure?

Subject: saving current figure?

From: Jessica

Date: 25 Jul, 2012 21:21:27

Message: 1 of 8

Hi,

Can anyone give suggestions about how to save the current figure with a specified DPI AND save it with the same dimensions it shows according to the axis. My current figure is 96 dpi and 1280 x 1024 pix and I want to save this (as a .jpg or .bmp) with the same properties. I also don't want to save the extra border around the figure (that is outside the figure handle).

Thanks

Subject: saving current figure?

From: Saravanan Mani

Date: 26 Jul, 2012 07:24:20

Message: 2 of 8

hello,
t = 0:0.001:10;
x = sin(2*pi*t);
fh = figure('Name','Test',...
            'numbertitle','off');
plot(t,x,'g');
msgf = questdlg('Do you want to save your figure window','YES','NO');
if length(msgf) > 2
    % we can our filename like example 'test' and which format you want
    % save like jpg
    saveas(gcf,'test','jpg');
elseif length(msgf) > 1
   disp(' do nothing')
end

Can you please try this and let me know
BR,
Saravanan Mani


"Jessica" wrote in message <jupo0m$kdk$1@newscl01ah.mathworks.com>...
> Hi,
>
> Can anyone give suggestions about how to save the current figure with a specified DPI AND save it with the same dimensions it shows according to the axis. My current figure is 96 dpi and 1280 x 1024 pix and I want to save this (as a .jpg or .bmp) with the same properties. I also don't want to save the extra border around the figure (that is outside the figure handle).
>
> Thanks

Subject: saving current figure?

From: Jessica

Date: 26 Jul, 2012 10:59:09

Message: 3 of 8

"Saravanan Mani" wrote in message <juqrb4$ijc$1@newscl01ah.mathworks.com>...
> hello,
> t = 0:0.001:10;
> x = sin(2*pi*t);
> fh = figure('Name','Test',...
> 'numbertitle','off');
> plot(t,x,'g');
> msgf = questdlg('Do you want to save your figure window','YES','NO');
> if length(msgf) > 2
> % we can our filename like example 'test' and which format you want
> % save like jpg
> saveas(gcf,'test','jpg');
> elseif length(msgf) > 1
> disp(' do nothing')
> end
>
> Can you please try this and let me know
> BR,
> Saravanan Mani
>

Unfortunately, the command "saveas(gcf,'test','jpg');" does not preserve the DPI and dimensions. I have found that "saveas(gca,'myfigure.eps','epsc');" works slightly better but I still have to (1) open the image in photoshop and adjust the dimensions and (2) the pixel dimensions are slightly off.
>
> "Jessica" wrote in message <jupo0m$kdk$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > Can anyone give suggestions about how to save the current figure with a specified DPI AND save it with the same dimensions it shows according to the axis. My current figure is 96 dpi and 1280 x 1024 pix and I want to save this (as a .jpg or .bmp) with the same properties. I also don't want to save the extra border around the figure (that is outside the figure handle).
> >
> > Thanks

Subject: saving current figure?

From: Oliver Woodford

Date: 26 Jul, 2012 13:48:14

Message: 4 of 8

"Jessica" wrote in message <jupo0m$kdk$1@newscl01ah.mathworks.com>...
> Hi,
>
> Can anyone give suggestions about how to save the current figure with a specified DPI AND save it with the same dimensions it shows according to the axis. My current figure is 96 dpi and 1280 x 1024 pix and I want to save this (as a .jpg or .bmp) with the same properties. I also don't want to save the extra border around the figure (that is outside the figure handle).
>
> Thanks

export_fig (http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig) does this if you save as png or tiff.

Subject: saving current figure?

From: Jessica

Date: 26 Jul, 2012 15:01:11

Message: 5 of 8

"Oliver Woodford" wrote in message <jurhqu$7tq$1@newscl01ah.mathworks.com>...
> "Jessica" wrote in message <jupo0m$kdk$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > Can anyone give suggestions about how to save the current figure with a specified DPI AND save it with the same dimensions it shows according to the axis. My current figure is 96 dpi and 1280 x 1024 pix and I want to save this (as a .jpg or .bmp) with the same properties. I also don't want to save the extra border around the figure (that is outside the figure handle).
> >
> > Thanks
>
> export_fig (http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig) does this if you save as png or tiff.

The figure I load is 1280 x 1024. When I save it with:

export_fig test3.png -r96

It outputs a figure with 96 dpi but with 684x547.

When I save it with:

export_fig test3.png -r96 -m2 or export_fig test3.png -r48 -m2

It outputs a figure with 180 dpi and 1281 x 1025.

Any suggestions for getting it to 96 dpi and 1280 x 1024?

Subject: saving current figure?

From: Jessica

Date: 26 Jul, 2012 18:29:15

Message: 6 of 8

"Jessica" wrote in message <jurm3n$q3k$1@newscl01ah.mathworks.com>...
> "Oliver Woodford" wrote in message <jurhqu$7tq$1@newscl01ah.mathworks.com>...
> > "Jessica" wrote in message <jupo0m$kdk$1@newscl01ah.mathworks.com>...
> > > Hi,
> > >
> > > Can anyone give suggestions about how to save the current figure with a specified DPI AND save it with the same dimensions it shows according to the axis. My current figure is 96 dpi and 1280 x 1024 pix and I want to save this (as a .jpg or .bmp) with the same properties. I also don't want to save the extra border around the figure (that is outside the figure handle).
> > >
> > > Thanks
> >
> > export_fig (http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig) does this if you save as png or tiff.
>
> The figure I load is 1280 x 1024. When I save it with:
>
> export_fig test3.png -r96
>
> It outputs a figure with 96 dpi but with 684x547.
>
> When I save it with:
>
> export_fig test3.png -r96 -m2 or export_fig test3.png -r48 -m2
>
> It outputs a figure with 180 dpi and 1281 x 1025.
>
> Any suggestions for getting it to 96 dpi and 1280 x 1024?


Also, is it possible to use export_fig to save the file with a certain name:

i=1; Filename=strcat('Picture',num2str(i),'.tiff');

export_fig Filename -r96

so that the file is called "Picture1" rather than "Filename"

Subject: saving current figure?

From: ImageAnalyst

Date: 26 Jul, 2012 23:01:43

Message: 7 of 8

On Thursday, July 26, 2012 2:29:15 PM UTC-4, Jessica wrote:
> Also, is it possible to use export_fig to save the file with a certain name:
>
> i=1; Filename=strcat('Picture',num2str(i),'.tiff');
>
> export_fig Filename -r96
>
> so that the file is called "Picture1" rather than "Filename"

-----------------------------------------
Use the function form
export_fig(Filename, '-r96');

Subject: saving current figure?

From: Oliver Woodford

Date: 27 Jul, 2012 08:29:09

Message: 8 of 8

"Jessica" wrote:
> > "Jessica" wrote in message <jupo0m$kdk$1@newscl01ah.mathworks.com>...
> > > Hi,
> > >
> > > Can anyone give suggestions about how to save the current figure with a specified DPI AND save it with the same dimensions it shows according to the axis. My current figure is 96 dpi and 1280 x 1024 pix and I want to save this (as a .jpg or .bmp) with the same properties. I also don't want to save the extra border around the figure (that is outside the figure handle).
> > >
> > > Thanks
> >
> > export_fig (http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig) does this if you save as png or tiff.
>
> The figure I load is 1280 x 1024. When I save it with:
>
> export_fig test3.png -r96
>
> It outputs a figure with 96 dpi but with 684x547.
>
> When I save it with:
>
> export_fig test3.png -r96 -m2 or export_fig test3.png -r48 -m2
>
> It outputs a figure with 180 dpi and 1281 x 1025.
>
> Any suggestions for getting it to 96 dpi and 1280 x 1024?

Hi Jessica

In MATLAB your figure doesn't have a dpi, only your screen does, and this is the dpi that export_fig uses. On my system the dpi is:
>> dpi = get(0, 'ScreenPixelsPerInch')

dpi =

    96

So when I do:
>> figure('position', [0 0 1280 1024]);
>> plot(rand(3));
>> Filename = 'test.png';
>> export_fig(Filename, '-a1', '-nocrop');
the output is a png with a dpi of 96 and a size of 1280x1024, just as you want, and export_fig works as expected.

Given that export_fig returns an image with a dpi of 96 on your system, it must also have its dpi correctly set to 96. Therefore the only conclusion I can come to is that your figure is not 1280x1024, as you claim, but is smaller. Perhaps you are getting confused between figure size and resolution of an image in a figure - there is a big difference.

If you want to export a figure at 1280x1024 and 96dpi you need to set the figure size to 1280x1024 using:
>> set(gcf, 'Position', [0 0 1280 1024]);
before resaving the figure.

HTH,
Oliver

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us