MATLAB Answers

Igor
0

How to print axes area on GUI?

Asked by Igor
on 27 May 2011
1. Axes on the GUI-window (not figure) 2. GUI-win property "Resizable=on" maybe set 3. I need to obtain jpg or bmp file with only axes area (plus annotations&title) at actual screen size. By simple way... 4. It's better to don't change GUI-win properties.
My attempts, 3 ways
function bmp_Callback(hObject, eventdata, handles)
% hObject handle to bmp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h=handles.axes1;rect=get(h,'OuterPosition');
photo=getframe(h,rect);[photo,cmp]=frame2im(photo);
imwrite(photo,'photo2.jpg','jpg','Quality',80);
beep;
% --------------------------------------------------------------------
function jpg_Callback(hObject, eventdata, handles)
% hObject handle to jpg (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
saveas(handles.output,'photo.jpg');
print('photo1','-noui','-djpeg','-r200');
beep;

  1 Comment

Igor
on 27 May 2011
remarks:
a) actual (screen) figure size as it seen -- that is small size
b) axes background color should be kept
...
like as capture rectangular snapshot by MATLAB tools

Sign in to comment.

2 Answers

Answer by Matt Fig
on 27 May 2011

This works on my computer, you may have to tweak a few things.
close all
figure('units','pix','pos',[200 200 850 750])
ax = axes;
plot((1:10).^2)
set(ax,'units','pix')
legend('x^2')
xlabel('X Label','fontsize',12)
ylabel('Y Label','fontsize',12)
title('Title','fontsize',24)
drawnow
% Now capture the axes and labels.
P = get(ax,'pos');
T = get(ax,'tightinset');
h = [P(1)-T(1)-5 P(2)-T(2)-5 P(3)+P(1)/2+T(3)+15 P(4)+P(2)/2+T(4)+10];
F = getframe(gcf,h);
[X,Map] = frame2im(F);
% I put the funny background color so the captured area stands out.
figure('color',[.6 .2 .2],'units','pix','pos',[26 33 1184 925])
image(X)
set(gca,'visible','off')
axis normal
imwrite(X,'myimage.jpg') % save the image.

  2 Comments

Igor
on 28 May 2011
I think similar but this works incorrectly :(
why the width/height are determined with error (a corner - valid)?
h=handles.axes1;unit=get(h,'Units');set(h,'Units','pixel');dh=2;
rect=get(h,'Position');drect=get(h,'TightInset');
rect([1 2])=-drect([1 2])-dh;rect([3 4])=rect([3 4])+drect([3 4])+dh;
photo=getframe(h,rect);[photo,cmp]=frame2im(photo);
imwrite(photo,'photo2.jpg','jpg','Quality',95);
set(h,'Units',unit);beep;
Matt Fig
on 28 May 2011
I was using the figure frame of reference and you were using the axes. Both can be made to work, with some adjustment.
I see now what you did, and I think you could make it work. Try modifying your code to this:
rect = get(h,'Position');
drect = get(h,'TightInset');
rect([3 4]) = rect([3 4]) + drect([3 4]) + rect([1 2])/2 +dh;
rect([1 2]) = - drect([1 2]) + dh;
That should work...

Sign in to comment.


Answer by Walter Roberson
on 27 May 2011

I suggest you consider the MATLAB File Exchange contribution export_fig written by Oliver.

  3 Comments

Matt Fig
on 27 May 2011
http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig
Igor
on 27 May 2011
It's too heavy way, also requiring files saving... no
The way by "imread" is seems to be more simple.
And fast at execution... the problem -- how to attenuate rectangle "rect"
Igor
on 27 May 2011
Thanks, of course :)
Already have been found
very wide opportunities... but too heavy

Sign in to comment.