views:

44

answers:

2

Hey guys,

Do you know what function the actual figure window uses when you go File>Save As... and then select .png or another image format?? Because I am trying to automate saving, but when I use saveas or print, the resulting image is really pixelated, but if I look at the actual figure, and use the File>Save As... method as above, and save as .png, the image looks really good.

What method should I use to save my figure from the command line?? Either the actual method that the figure window would work or if you guys have better solutions I'd appricate it! :)

Thanks,

tylerthemiler

+1  A: 

I don't know about you, but for me the saved image looks nice.

Code example:

... create some figure ...
saveas(gcf, 'some_figure.png');

To set a user specified resolution use:

print(gcf,'some_figure','-dpng','-rSOMENUMBER')

where SOMENUMBERis a parameter used for the resolution. The final resolution is (SOMENUMBER*8) x (SOMENUMBER*6)

George B.
+4  A: 

The callback for the "Save As..." menu item invokes the function FILEMENUFCN with the first input argument being the handle of the figure the menu is in and the second input argument being the string 'FileSaveAs'. If you have the figure handle stored in the variable hFigure, then the following command should be equivalent to clicking the "Save As..." menu item in that figure window:

>> filemenufcn(hFigure,'FileSaveAs');


A few notes...

  • The function FILEMENUFCN is only partially documented. You can do help filemenufcn in the command window, but there is no entry for it in the online documentation. In MATLAB 2009a, the function can be found in the following folder:

    C:\Program Files\MATLAB\R2009a\toolbox\matlab\uitools\filemenufcn.m
    

    Looking through the function code, it appears that it ultimately calls either the function SAVEAS for .fig files or the function HGEXPORT (with additional input arguments) for other file types.

  • I was able to hunt down the callback for the "Save As..." menu item by searching through the children of the figure window and its menus. You can do this yourself by setting the root property 'ShowHiddenHandles' to 'on' and then traversing through the 'Children' properties of the figure window and its menus using the GET command. An alternative is to use the FINDALL command, assuming you know some properties of the objects you are looking for. For example, this will find the handle to the "File" menu for the current figure window:

    >> hFileMenu = findall(gcf,'Label','&File');
    

    And this will find the handle to the "Save As..." menu item and display its callback:

    >> hSaveAs = findall(hFileMenu,'Label','Save &As...');
    >> get(hSaveAs,'Callback')
    ans =
    filemenufcn(gcbf,'FileSaveAs')
    
gnovice
Thanks, you put me on the right track! I ended up using this: http://www.mathworks.com/matlabcentral/newsreader/view_thread/239960Setting options.Format = 'png' Saved it without a lot of pixelation.
tylerthemiler
@tylerthemiler: Yes, it appears that the function [HGEXPORT](http://www.mathworks.com/help/techdoc/ref/hgexport.html) is ultimately called (with some extra input arguments as shown in that newsgroup thread) when saving PNG images.
gnovice
Thanks! QOTSA rocks btw.
tylerthemiler

related questions