How to imwrite ONLY the cdata portion of a figure

10 views (last 30 days)
I am attempting to save a series of images (a gif in this case) for use in a powerpoint presentation. I do not want the x-axis, y-axis, title, colorbar, etc. I understand that these can be disabled using axis off. However, this still leaves an unattractive grey border around the figure. This is especially undesirable for powerpoint where I may place the resulting image on background. I am looking for a way to capture only the relevant part of the figure, the actual data.
Again, since I plan on using this for generating e.g. gifs I am utilizing imwrite to save the figures. Since these may be saved in a gif stack, manually cropping figures afterwards is frustrating.
Note also that I use axis equal before hand, due to the nature of images I am plotting.
There are a variety of solutions I have found, none of which do what I want. These include:
  • axis off - Leaves grey box.
  • axis tight - After axis off still leaves grey box.
  • set(gca,'position',[0 0 1 1],'units','normalized'); - Removes grey space at top and bottom. However, since I used axis equal, and the plot does not stretch to fill the frame, this leaves grey bars in either side. (Note, it does work if the axis is in auto mode, instead of equal. Unfortunately, they must be on an equal spacing for this application. I cannot guarantee the image will be square, however).
  • Some more exotic ideas all involving manually resizing the window.
I assume there must be a way to do this, since creating Matlab plot without the wasted space of the rest of the frame for use in other applications seems like an obvious need. Am I missing something really simple?
As always, apologies if the answer exists elsewhere, and I will include a MWE below.
function tightPlotMWE
data = rand(100);
hf = figure(1);
filename = 'tightPlotFigure.png';
% set(gca,'position',[0 0 1 1],'units','normalized');
iptsetpref('ImshowBorder','tight');
imagesc(data); %axis equal
axis tight off
frame = getframe(gcf);
[imind,cm] = rgb2ind(frame.cdata,256,'nodither');
imwrite(imind,cm,filename,'png');
Thanks Again!

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jul 2016
Use the FreezeColors File Exchange contribution, and then get() the CData property of the image object created by imagesc() . You will not need to use getframe()

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!