Ignore:
Timestamp:
09/21/15 13:50:48 (10 years ago)
Author:
Mathieu Morlighem
Message:

CHG: new version of export_fig

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/externalpackages/export_fig/isolate_axes.m

    r18904 r19571  
     1function fh = isolate_axes(ah, vis)
    12%ISOLATE_AXES Isolate the specified axes in a figure on their own
    23%
     
    2223
    2324% Thank you to Rosella Blatt for reporting a bug to do with axes in GUIs
    24 % 16/3/2012 Moved copyfig to its own function. Thanks to Bob Fratantonio
    25 % for pointing out that the function is also used in export_fig.m.
    26 % 12/12/12 - Add support for isolating uipanels. Thanks to michael for
    27 % suggesting it.
    28 % 08/10/13 - Bug fix to allchildren suggested by Will Grant (many thanks!).
    29 % 05/12/13 - Bug fix to axes having different units. Thanks to Remington
    30 % Reid for reporting the issue.
     25% 16/03/12: Moved copyfig to its own function. Thanks to Bob Fratantonio
     26%           for pointing out that the function is also used in export_fig.m
     27% 12/12/12: Add support for isolating uipanels. Thanks to michael for suggesting it
     28% 08/10/13: Bug fix to allchildren suggested by Will Grant (many thanks!)
     29% 05/12/13: Bug fix to axes having different units. Thanks to Remington Reid for reporting
     30% 21/04/15: Bug fix for exporting uipanels with legend/colorbar on HG1 (reported by Alvaro
     31%           on FEX page as a comment on 24-Apr-2014); standardized indentation & help section
     32% 22/04/15: Bug fix: legends and colorbars were not exported when exporting axes handle in HG2
    3133
    32 function fh = isolate_axes(ah, vis)
    33 % Make sure we have an array of handles
    34 if ~all(ishandle(ah))
    35     error('ah must be an array of handles');
    36 end
    37 % Check that the handles are all for axes or uipanels, and are all in the same figure
    38 fh = ancestor(ah(1), 'figure');
    39 nAx = numel(ah);
    40 for a = 1:nAx
    41     if ~ismember(get(ah(a), 'Type'), {'axes', 'uipanel'})
    42         error('All handles must be axes or uipanel handles.');
     34    % Make sure we have an array of handles
     35    if ~all(ishandle(ah))
     36        error('ah must be an array of handles');
    4337    end
    44     if ~isequal(ancestor(ah(a), 'figure'), fh)
    45         error('Axes must all come from the same figure.');
     38    % Check that the handles are all for axes or uipanels, and are all in the same figure
     39    fh = ancestor(ah(1), 'figure');
     40    nAx = numel(ah);
     41    for a = 1:nAx
     42        if ~ismember(get(ah(a), 'Type'), {'axes', 'uipanel'})
     43            error('All handles must be axes or uipanel handles.');
     44        end
     45        if ~isequal(ancestor(ah(a), 'figure'), fh)
     46            error('Axes must all come from the same figure.');
     47        end
    4648    end
    47 end
    48 % Tag the objects so we can find them in the copy
    49 old_tag = get(ah, 'Tag');
    50 if nAx == 1
    51     old_tag = {old_tag};
    52 end
    53 set(ah, 'Tag', 'ObjectToCopy');
    54 % Create a new figure exactly the same as the old one
    55 fh = copyfig(fh); %copyobj(fh, 0);
    56 if nargin < 2 || ~vis
    57     set(fh, 'Visible', 'off');
    58 end
    59 % Reset the object tags
    60 for a = 1:nAx
    61     set(ah(a), 'Tag', old_tag{a});
    62 end
    63 % Find the objects to save
    64 ah = findall(fh, 'Tag', 'ObjectToCopy');
    65 if numel(ah) ~= nAx
    66     close(fh);
    67     error('Incorrect number of objects found.');
    68 end
    69 % Set the axes tags to what they should be
    70 for a = 1:nAx
    71     set(ah(a), 'Tag', old_tag{a});
    72 end
    73 % Keep any legends and colorbars which overlap the subplots
    74 lh = findall(fh, 'Type', 'axes', '-and', {'Tag', 'legend', '-or', 'Tag', 'Colorbar'});
    75 nLeg = numel(lh);
    76 if nLeg > 0
    77     set([ah(:); lh(:)], 'Units', 'normalized');
    78     ax_pos = get(ah, 'OuterPosition');
    79     if nAx > 1
    80         ax_pos = cell2mat(ax_pos(:));
     49    % Tag the objects so we can find them in the copy
     50    old_tag = get(ah, 'Tag');
     51    if nAx == 1
     52        old_tag = {old_tag};
    8153    end
    82     ax_pos(:,3:4) = ax_pos(:,3:4) + ax_pos(:,1:2);
    83     leg_pos = get(lh, 'OuterPosition');
    84     if nLeg > 1;
    85         leg_pos = cell2mat(leg_pos);
     54    set(ah, 'Tag', 'ObjectToCopy');
     55    % Create a new figure exactly the same as the old one
     56    fh = copyfig(fh); %copyobj(fh, 0);
     57    if nargin < 2 || ~vis
     58        set(fh, 'Visible', 'off');
    8659    end
    87     leg_pos(:,3:4) = leg_pos(:,3:4) + leg_pos(:,1:2);
    88     ax_pos = shiftdim(ax_pos, -1);
    89     % Overlap test
    90     M = bsxfun(@lt, leg_pos(:,1), ax_pos(:,:,3)) & ...
    91         bsxfun(@lt, leg_pos(:,2), ax_pos(:,:,4)) & ...
    92         bsxfun(@gt, leg_pos(:,3), ax_pos(:,:,1)) & ...
    93         bsxfun(@gt, leg_pos(:,4), ax_pos(:,:,2));
    94     ah = [ah; lh(any(M, 2))];
    95 end
    96 % Get all the objects in the figure
    97 axs = findall(fh);
    98 % Delete everything except for the input objects and associated items
    99 delete(axs(~ismember(axs, [ah; allchildren(ah); allancestors(ah)])));
     60    % Reset the object tags
     61    for a = 1:nAx
     62        set(ah(a), 'Tag', old_tag{a});
     63    end
     64    % Find the objects to save
     65    ah = findall(fh, 'Tag', 'ObjectToCopy');
     66    if numel(ah) ~= nAx
     67        close(fh);
     68        error('Incorrect number of objects found.');
     69    end
     70    % Set the axes tags to what they should be
     71    for a = 1:nAx
     72        set(ah(a), 'Tag', old_tag{a});
     73    end
     74    % Keep any legends and colorbars which overlap the subplots
     75    % Note: in HG1 these are axes objects; in HG2 they are separate objects, therefore we
     76    %       don't test for the type, only the tag (hopefully nobody but Matlab uses them!)
     77    lh = findall(fh, 'Tag', 'legend', '-or', 'Tag', 'Colorbar');
     78    nLeg = numel(lh);
     79    if nLeg > 0
     80        set([ah(:); lh(:)], 'Units', 'normalized');
     81        try
     82            ax_pos = get(ah, 'OuterPosition'); % axes and figures have the OuterPosition property
     83        catch
     84            ax_pos = get(ah, 'Position'); % uipanels only have Position, not OuterPosition
     85        end
     86        if nAx > 1
     87            ax_pos = cell2mat(ax_pos(:));
     88        end
     89        ax_pos(:,3:4) = ax_pos(:,3:4) + ax_pos(:,1:2);
     90        try
     91            leg_pos = get(lh, 'OuterPosition');
     92        catch
     93            leg_pos = get(lh, 'Position');  % No OuterPosition in HG2, only in HG1
     94        end
     95        if nLeg > 1;
     96            leg_pos = cell2mat(leg_pos);
     97        end
     98        leg_pos(:,3:4) = leg_pos(:,3:4) + leg_pos(:,1:2);
     99        ax_pos = shiftdim(ax_pos, -1);
     100        % Overlap test
     101        M = bsxfun(@lt, leg_pos(:,1), ax_pos(:,:,3)) & ...
     102            bsxfun(@lt, leg_pos(:,2), ax_pos(:,:,4)) & ...
     103            bsxfun(@gt, leg_pos(:,3), ax_pos(:,:,1)) & ...
     104            bsxfun(@gt, leg_pos(:,4), ax_pos(:,:,2));
     105        ah = [ah; lh(any(M, 2))];
     106    end
     107    % Get all the objects in the figure
     108    axs = findall(fh);
     109    % Delete everything except for the input objects and associated items
     110    delete(axs(~ismember(axs, [ah; allchildren(ah); allancestors(ah)])));
    100111end
    101112
    102113function ah = allchildren(ah)
    103 ah = findall(ah);
    104 if iscell(ah)
    105     ah = cell2mat(ah);
    106 end
    107 ah = ah(:);
     114    ah = findall(ah);
     115    if iscell(ah)
     116        ah = cell2mat(ah);
     117    end
     118    ah = ah(:);
    108119end
    109120
    110121function ph = allancestors(ah)
    111 ph = [];
    112 for a = 1:numel(ah)
    113     h = get(ah(a), 'parent');
    114     while h ~= 0
    115         ph = [ph; h];
    116         h = get(h, 'parent');
     122    ph = [];
     123    for a = 1:numel(ah)
     124        h = get(ah(a), 'parent');
     125        while h ~= 0
     126            ph = [ph; h];
     127            h = get(h, 'parent');
     128        end
    117129    end
    118130end
    119 end
Note: See TracChangeset for help on using the changeset viewer.