Home > atmlab > graphs > splitax.m

splitax

PURPOSE ^

splitax Split axes h in N vertical subplots

SYNOPSIS ^

function splitax(oldax, N, varargin)

DESCRIPTION ^

 splitax Split axes h in N vertical subplots

 Cuts x-axis in N equally sized parts and plots data from axes h in N
 subplots below each other. Will create a new figure.

 FORMAT

   splitax(ax, N[, opts])

 IN

   ax      handle to axes to be copied
   N       number of subplots to split it in
   opts    structure with options:
       legend      copy legend, defaulte false

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

splitax.m

SOURCE CODE ^

0001 function splitax(oldax, N, varargin)
0002 
0003 % splitax Split axes h in N vertical subplots
0004 %
0005 % Cuts x-axis in N equally sized parts and plots data from axes h in N
0006 % subplots below each other. Will create a new figure.
0007 %
0008 % FORMAT
0009 %
0010 %   splitax(ax, N[, opts])
0011 %
0012 % IN
0013 %
0014 %   ax      handle to axes to be copied
0015 %   N       number of subplots to split it in
0016 %   opts    structure with options:
0017 %       legend      copy legend, defaulte false
0018 %
0019 %
0020 
0021 % $Id: splitax.m 7900 2012-09-26 16:28:14Z gerrit $
0022 
0023 extra = optargs(varargin, {struct()});
0024 opts = optargs_struct(extra, ...
0025     struct('legend', 0));
0026 
0027 fig = figure('Visible', get(get(oldax, 'Parent'), 'Visible'));
0028 newax = zeros(1, N);
0029 oldlim = get(oldax, 'XLim');
0030 oldrange = oldlim(2) - oldlim(1);
0031 
0032 if opts.legend
0033     Ntot = N+1;
0034 else
0035     Ntot = N;
0036 end
0037 
0038 for i = 1:N
0039     newax(i) = subplot(Ntot, 1, i);
0040     % copy all children of 'h' to new subplot
0041     copyobj(get(oldax, 'Children'), newax(i));
0042     % set xlim correctly
0043     set(newax(i), 'xlim', [oldlim(1) + (i-1)*oldrange/N, ...
0044                            oldlim(2) - (N-i)*oldrange/N], ...
0045                   'XGrid', get(oldax, 'XGrid'), ...
0046                   'YGrid', get(oldax, 'YGrid'));
0047     % copy labels
0048     %xlabel(get(get(h, 'XLabel'), 'String'));
0049     ylabel(get(get(oldax, 'YLabel'), 'String'));
0050     ylim(get(oldax, 'YLim'));
0051 end
0052 
0053 if (opts.legend)
0054     leg = findobj('Tag', 'legend', 'Parent', get(oldax, 'Parent'));
0055     if isempty(leg)
0056         error(['atmlab:' mfilename ':nolegend'], ...
0057             ['Legend was requested to be copied ' ...
0058             'but no legend was found in ' ...
0059             'parent figure!']);
0060     end
0061     copyobj(leg, fig);
0062     newleg = findobj('Tag', 'legend', 'Parent', fig);
0063     % make invisible new subplot to check where to place legend
0064     sp = subplot(Ntot, 1, Ntot);
0065     set(sp, 'Visible', 'off');
0066     spNpos = get(sp, 'Position');
0067     legpos = get(newleg, 'Position');
0068     legpos(1) = spNpos(1);
0069     legpos(2) = 0;
0070     set(newleg, 'Position', legpos);
0071 end
0072 
0073 %xlabel(get(get(h, 'XLabel'), 'String'));
0074 
0075 % suptitle calls axes() which sets figure to visible, may not want that!
0076 vis = get(gcf(), 'Visible');
0077 suptitle(get(get(oldax, 'Title'), 'String'));
0078 set(gcf(), 'Visible', vis);
0079 %htax = get(ht, 'Parent');
0080 xlabel(newax(N), get(get(oldax, 'XLabel'), 'String'));
0081 %ylabel(htax, get(get(h, 'YLabel'), 'String'));
0082 
0083 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005