7

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 http://goo.gl/p907wR

motiur
  • 1,640
  • 9
  • 33
  • 61
  • 3
    Is `saveas(gcf,'filename','format')` what you want? Try a vectorized format as .eps – Werner Aug 16 '13 at 04:31
  • 1
    @Werner: Thanks for answering , could you please elaborate . – motiur Aug 16 '13 at 04:40
  • 1
    @Werner: How do I get rid of the axes ? – motiur Aug 16 '13 at 04:54
  • 1
    What do you mean the axes? You dont want the ticks? Do `set(gca,'XTick',[])` `set(gca,'YTick',[])`. I think you may want to set the background as white also: `set(gcf,'Color','w')` – Werner Aug 16 '13 at 05:21
  • 2
    That will be it , for now . – motiur Aug 16 '13 at 05:26
  • 1
    possible duplicate of [Plotting and Saving as File in MATLAB](http://stackoverflow.com/questions/7617843/plotting-and-saving-as-file-in-matlab) – Werner Aug 16 '13 at 06:23
  • 1
    @Werner: Do you really have to say whether this is a duplicate or not ; nice of you that you pointed .I am adding your reply as a possible answer . – motiur Aug 16 '13 at 08:33
  • 1
    Well, I am not saying that you didn't research before your question, just that you were unfortunate. Anyway, if people decide that this is not a duplicate, let's facilitate the things x) – Werner Aug 16 '13 at 15:25

3 Answers3

14

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

Werner
  • 2,537
  • 1
  • 26
  • 38
  • 1
    Well I couldn't avoid the ticks from being shown in my image , and there was a white border around the image as a frame , which is also I didn't want . But I am satisfied that my I have a plausible answer to my schools assignment .Thanks again . – motiur Aug 16 '13 at 15:46
  • 1
    @MotiurRahman Added an example. Beware that the gca changes if you plot another figure or axes. – Werner Aug 16 '13 at 15:57
  • 1
    Hugs for the effort , your example works , but I am in Archlinux and nautilus is framing an white border around the image , even though I am not getting the axis right . Thing is I have to do an average filtering of this image , and the white border( which can not be recognized , because the webpage is white ) is really messing up my image( which is actually a grainy image , not this 3x3 image above).So I have to resort to median filtering , which is okayish, but average filtering would have been better . – motiur Aug 16 '13 at 16:07
  • 2
    My image also have the white border. To remove it, make the axes occupy the hole figure: `set(gca,'Position',[0 0 1 1])` – Werner Aug 16 '13 at 16:21
  • 2
    Haha , you are just relentless , finally I am happy . – motiur Aug 16 '13 at 16:56
  • Congratulations on your intervention. It was of immense relevance for our work. Just one note, please try to provide information regarding the `imagesc(magic(3))` code line. – Francisco Maria Calisto Apr 17 '19 at 14:21
4

You can use:

print -djpeg99 'foo.jpg'

This will save it as 'foo.jpg' as you need.

ntg
  • 12,950
  • 7
  • 74
  • 95
2

You can use the following code

 imagesc(A);
 %%saving the image
 hgexport(gcf, 'figure1.jpg', hgexport('factorystyle'), 'Format', 'jpeg');
 set(gcf,'PaperUnits','inches','PaperPosition',[0 0 4 4]);
 print -djpeg filename.jpg -r10

Here A will be the matrix from which you will have an image. And the image will be saved as filename.jpg in the directory.