matlabmatlab-figure

Save exact image output from imagesc in matlab


Rainbow

Hi , I want to save this image produced from imagesc(magic(3)), the exact rainbow representation, is it possible?

Thanks.

This question might look like a duplicate , but it is not . I looked at the solution to the similar question at this site , but it did not satisfy me . I have looked into the Matlab help center and the close answer that I got was this one , at the bottom of this page


Solution

  • To save the figure as a file (don't matter how it was created), one should do:

    saveas(figureHandle,'filename','format')
    

    where figureHandle could be the gcf handle, which means: get current figure.

    As pointed in the discussion, if someone doesn't want the ticks to be shown, the person can add:

    set(gca,'XTick',[])
    set(gca,'YTick',[])
    

    where gca is the handle to the current axis, just as gcf. If you have more than one axis, don't forget to "handle the handles". They are returned to you when you create them, i.e.:

    hFig = figure(pairValuedProperties); % Create and get the figure handle
    hAxes1 = suplot(2,1,1,pairValuedProperties); % Create and get the upper axes handle
    hAxes2 = suplot(2,1,2,pairValuedProperties); % Create and get the bottom axes handle
    

    where the pair value are the figure or axes properties declared in the following syntax:

    'PropertyName1',PropertyValue1,'PropertyName2',PropertyValue2,…

    Here are the matlab documentation about the Figure and Axes Properties, and about the saveas method.


    Example:

    The image saved with the following code:

    figure 
    imagesc(magic(3))
    set(gca,'XTick',[]) % Remove the ticks in the x axis!
    set(gca,'YTick',[]) % Remove the ticks in the y axis
    set(gca,'Position',[0 0 1 1]) % Make the axes occupy the hole figure
    saveas(gcf,'Figure','png')
    

    Figure without the white border