开发者

In matlab, how do you save a figure as an image in the same way as using "Save As..." in the figure window?

When saving a figure, what function does Matlab use? For example, when a user selects File > Save As..开发者_开发技巧. and then selects .png or another image format, what is going on behind the scenes?

I am asking because I am trying to automate saving, but when I use saveas or print, the resulting image is really pixelated. However, the image looks really good when I save the figure using the method described above.

What method should I use to save my figure from the command line? The actual method that the figure window uses would work, but if you guys have better solutions, I'd appricate it!


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')
    


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜