saving png with the correct format (legend)

14 views (last 30 days)
Mindy
Mindy on 19 Apr 2013
I am saving .fig to .png using saveas. I can't get the png output looks exactly like what the .fig shows on the screen. I have tried the set the .fig size to 1200 by 900 before saving, but the png output still looks like the default size. So the legend ends up taking 1/3 of the png space, and became a useless chart for me.
bmp works, but it saves too slow so when I automate the process to save many plots, it doesn't work. Plus, the size of file is too big.
I have been struggling with it for awhile, assistance will be appreciated

Answers (2)

Image Analyst
Image Analyst on 19 Apr 2013

Shaun VanWeelden
Shaun VanWeelden on 19 Apr 2013
Also, check out any screenshot mfiles, most will figure out the position of the figure if you give it the figure handle. They will take a screenshot of exactly what is on the screen, just like pressing the "print screen" button on your keyboard would do, but just for the figure window.
My version I have used is here: (and if I remember correctly, you can just give it the values returned by get(fHandle,'position') for the rect. and it should work.
function [img] = getScreenShot(rect)
%example rect: [200 300 600 400]
%This could use a LOT of improvement!
screensize=get(0,'monitorpositions');
width=screensize(3);
height=screensize(4);
if rect<1
%they used normalized units
rect([1 3])=rect([1 3]).*width;
rect([2 4])=rect([2 4]).*height;
end
rect=max(rect,1);
rect([1 3])=min(rect([1 3]),width);
rect([2 4])=min(rect([2 4]),height);
rect=round(rect);
robo = java.awt.Robot;
target=java.awt.Rectangle(0,0,width,height);
image = robo.createScreenCapture(target); %take screenshot at rect
rasta=image.getRGB(0,0,width,height,[],0,width); %get RGB data from bufferedimage
rasta=256^3+rasta;
B=uint8(mod(rasta,256));
G=uint8(mod((rasta-int32(B))./256,256));
R=uint8(mod((rasta-256*int32(G))./65536,256));
X.cdata=uint8(zeros(height,width,3));
X.cdata(:,:,1)=reshape(R,[width height])';
X.cdata(:,:,2)=reshape(G,[width height])';
X.cdata(:,:,3)=reshape(B,[width height])';
img=X.cdata(height-rect(2)-rect(4):height-rect(2),rect(1):rect(1)+rect(3)-1,:);
end

Community Treasure Hunt

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

Start Hunting!