Thursday, October 1, 2009

Matlab TightAxis Function

During one of my graduate classes I decided to create a function that makes a window that turns off all the Matlab figure toolbars and makes the axis fill the window AND scales the window to fit the image's native resolution so that no pixilization or aliasing occurs. If the image does not fit in the monitor's native resolution then the function will keep the current window size. When calling the TightAxis function make sure the current axis is the axis you want to modify as I use:
gca

If the axis you are currently interacting with is the one you want to modify just issue the command:
TightAxis(FIGURENAME)

Where FIGURENAME is a string that will be used to label the figure window.
Here is the function:

function TightAxis(title)
%First create an axis if
%the user doesn't have a gca
%and get the handle
haxis=gca;
%Best Image Fit Algorithm
set(gcf,'Visible','off')
set(haxis,'Position',[0,0,1,1])
monitorRes=get(0,'ScreenSize');
imageSize=get(haxis,'PlotBoxAspectRatio');
pos=get(gcf,'Position');
%Check for over sized images
%IF the native resolution of
%the image fits in the screen resolution
%AND the image does not contain a
%1-pixel dimension
%THEN scale the image to be displayed
%at 1x Resolution
if (imageSize(1)<=monitorRes(3)
&& imageSize(1)>1
&& imageSize(2)<=monitorRes(4)
&& imageSize(1)>1)
set(gcf,'Position',[0,0,imageSize(1:2)])
else
set(gcf,'Position',[0,0,pos(3:4)])
end
set(gcf,'Name',title,'NumberTitle','off')
set(gcf,'MenuBar','none')
zoom on;
movegui(gcf,'center')
set(gcf,'Visible','on')

2 comments: