www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/printlayout1.m

    function bH = printlayout1(axes2copyH, text2copyH, titlestring, fH)
%PRINTLAYOUT1

%  Copyright 2000-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.


% Test print for local models
% axes2copyH could be an axis handle or a gridlayout
% text2copyH could be am axis handle or a layout

if nargin == 0
   i_resize(gcbf);
   return;
end

% Make sure that the figure starts off invisible
if nargin < 4
    fH = figure('Visible','off');
end

set(fH,'WindowStyle', 'normal', 'Visible','off', 'Units','pixels', 'Color','w');
% Ensure that h contains only handles otherwise concatenation will fail
h = [];
if isgraphics(axes2copyH)
   % only print visible axes
   axes2copyH = findobj(axes2copyH,'flat','Visible','on');
   h = [h;axes2copyH(:)];
end
if isgraphics(text2copyH)
   h = [h;text2copyH(:)];
end

% Get axes units before converting to pixels
% Note need to convert to pixels in case one axes has
% normalised units, in which case it's size will not be correct
U = get(h,{'Units'});
set(h,'Units','pixels');
% Copy the objects
aH = copyobj(axes2copyH, fH);

% Have we been passed an array of handles
if (length(aH) > 1) && all(isgraphics(aH))
   % puts all axes into wrappers and then into a grid
   aH = i_CreateGrid(aH, fH);
elseif (length(aH) == 1) && isgraphics(aH) 
   aH = mbcgui.widget.AxesPanel('AxesHandle', aH, 'Border', [80 30 30 20]);
end
tH = copyobj(text2copyH, fH);

% Ensure no callbacks are left on the objects we copied
hAllChild = findobj(fH);
try 
    set(hAllChild, 'ButtonDownFcn', '', 'CreateFcn', '', 'DeleteFcn', '');
end

% Reset original handles units
set(h,{'Units'},U);
% Ensure that tH actually contains something
if isempty(tH)
   tH = xregcontainer(fH);
end
% Get the width and height of the two copied elements
aHwh = i_Pos2WH(get(aH, 'Position'));
tHwh = i_Pos2WH(get(tH, 'Position'));

% Add the title to the Layout
titleH = axestext(fH,'string',titlestring,'fontsize',11,'fontweight','bold',......
   'HorizontalAlignment','center','VerticalAlignment','middle','units','pixels',...
   'interpreter','none');

% find height of title - may be multiline
titleExt = get(titleH,'Extent');
titleHeight = titleExt(end)+10;
% Layout the final printed page
bH = xreggridbaglayout(fH,...
   'dimension',[3,3],...
   'colsizes',[20,-1,20],...
   'rowsizes',[titleHeight,-1,tHwh(2)],...
   'border', [0 20 0 0], ...
   'mergeblock',{[1 1],[1 3]},...%title all one row.
   'mergeblock',{[2 2],[1 3]},...%axes all one row.
   'elements',{...
      titleH,aH,[],...
      [],[],tH,...
      [],[],[]});
set(bH,'container',fH,'packstatus','on');

% Add the final layout to the userdata for resize purposes
ud.bH = bH;   
% Set the figure 
set(fH,'UserData',ud,...
   'ResizeFcn',mfilename,...
   'PaperPositionMode','auto',...
   'Position',[100 50 max(aHwh(1),tHwh(1)) aHwh(2)+tHwh(2)+titleHeight+70]);

% for development - if you want to check what the lyt looks like
% set(fH,'visible','on');

i_resize(fH);
printdlg(fH);

if isgraphics(fH)
    delete(fH); 
end


%-------------------------------------------------------------------
% SUBFUNCTION i_Pos2TL
%-------------------------------------------------------------------
function TL = i_Pos2TL(TLWH)
TL = TLWH(1:2);

%-------------------------------------------------------------------
% SUBFUNCTION i_Pos2WH
%-------------------------------------------------------------------
function WH = i_Pos2WH(TLWH)
WH = TLWH(3:4);

%-------------------------------------------------------------------
% SUBFUNCTION i_resize
%-------------------------------------------------------------------
function i_resize(fH)
% Is the new size bigger than the papersize?
u = get(fH,'Units');
% Set the units to be the paper units for comparison
set(fH,'Units','centimeter');
set(fH,'PaperUnits','centimeter');
s = get(fH,'PaperSize')-2;
p = get(fH,'Position');
% Compare the sizes
if any(i_Pos2WH(p) > s)
   set(fH,'Position',[i_Pos2TL(p) min(i_Pos2WH(p),s)]);
else
   set(fH,'Position',[i_Pos2TL(p) s]);
end
   
% Reset the figure units
set(fH,'Units',u);
% Repack the container
ud = get(fH,'UserData');
repack(ud.bH);

%-------------------------------------------------------------------
% SUBFUNCTION i_CreateGrid
%-------------------------------------------------------------------
function grid = i_CreateGrid(aH, fH)

num = length(aH);
nrows = ceil(sqrt(num));
ncols = round(sqrt(num));
aWH = i_Pos2WH(get(aH(1),'Position'));

NewAx = cell(1, num);
for i = 1:num
   NewAx{i}= mbcgui.widget.AxesPanel('AxesHandle', aH(i), 'Border', [60 30 20 20]);
end

height = nrows*(aWH(2));
width = ncols*(aWH(1));
grid = xreggridlayout(fH,...
   'dimension',[nrows ncols],...
   'elements',NewAx,...
   'rowsizes', -1*ones(nrows,1),...
   'colsizes', -1*ones(ncols,1),...
   'position',[0 0 width height]);