Switch to unified view

a b/combinedDeepLearningActiveContour/functions/tightfig.m
1
function hfig = tightfig(hfig)
2
% tightfig: Alters a figure so that it has the minimum size necessary to
3
% enclose all axes in the figure without excess space around them.
4
% 
5
% Note that tightfig will expand the figure to completely encompass all
6
% axes if necessary. If any 3D axes are present which have been zoomed,
7
% tightfig will produce an error, as these cannot easily be dealt with.
8
% 
9
% hfig - handle to figure, if not supplied, the current figure will be used
10
% instead.
11
12
    if nargin == 0
13
        hfig = gcf;
14
    end
15
16
    % There can be an issue with tightfig when the user has been modifying
17
    % the contnts manually, the code below is an attempt to resolve this,
18
    % but it has not yet been satisfactorily fixed
19
%     origwindowstyle = get(hfig, 'WindowStyle');
20
    set(hfig, 'WindowStyle', 'normal');
21
    
22
    % 1 point is 0.3528 mm for future use
23
24
    % get all the axes handles note this will also fetch legends and
25
    % colorbars as well
26
    hax = findall(hfig, 'type', 'axes');
27
    
28
    % get the original axes units, so we can change and reset these again
29
    % later
30
    origaxunits = get(hax, 'Units');
31
    
32
    % change the axes units to cm
33
    set(hax, 'Units', 'centimeters');
34
    
35
    % get various position parameters of the axes
36
    if numel(hax) > 1
37
%         fsize = cell2mat(get(hax, 'FontSize'));
38
        ti = cell2mat(get(hax,'TightInset'));
39
        pos = cell2mat(get(hax, 'Position'));
40
    else
41
%         fsize = get(hax, 'FontSize');
42
        ti = get(hax,'TightInset');
43
        pos = get(hax, 'Position');
44
    end
45
    
46
    % ensure very tiny border so outer box always appears
47
    ti(ti < 0.1) = 0.15;
48
    
49
    % we will check if any 3d axes are zoomed, to do this we will check if
50
    % they are not being viewed in any of the 2d directions
51
    views2d = [0,90; 0,0; 90,0];
52
    
53
    for i = 1:numel(hax)
54
        
55
        set(hax(i), 'LooseInset', ti(i,:));
56
%         set(hax(i), 'LooseInset', [0,0,0,0]);
57
        
58
        % get the current viewing angle of the axes
59
        [az,el] = view(hax(i));
60
        
61
        % determine if the axes are zoomed
62
        iszoomed = strcmp(get(hax(i), 'CameraViewAngleMode'), 'manual');
63
        
64
        % test if we are viewing in 2d mode or a 3d view
65
        is2d = all(bsxfun(@eq, [az,el], views2d), 2);
66
               
67
        if iszoomed && ~any(is2d)
68
           error('TIGHTFIG:haszoomed3d', 'Cannot make figures containing zoomed 3D axes tight.') 
69
        end
70
        
71
    end
72
    
73
    % we will move all the axes down and to the left by the amount
74
    % necessary to just show the bottom and leftmost axes and labels etc.
75
    moveleft = min(pos(:,1) - ti(:,1));
76
    
77
    movedown = min(pos(:,2) - ti(:,2));
78
    
79
    % we will also alter the height and width of the figure to just
80
    % encompass the topmost and rightmost axes and lables
81
    figwidth = max(pos(:,1) + pos(:,3) + ti(:,3) - moveleft);
82
    
83
    figheight = max(pos(:,2) + pos(:,4) + ti(:,4) - movedown);
84
    
85
    % move all the axes
86
    for i = 1:numel(hax)
87
        
88
        set(hax(i), 'Position', [pos(i,1:2) - [moveleft,movedown], pos(i,3:4)]);
89
        
90
    end
91
    
92
    origfigunits = get(hfig, 'Units');
93
    
94
    set(hfig, 'Units', 'centimeters');
95
    
96
    % change the size of the figure
97
    figpos = get(hfig, 'Position');
98
    
99
    set(hfig, 'Position', [figpos(1), figpos(2), figwidth, figheight]);
100
    
101
    % change the size of the paper
102
    set(hfig, 'PaperUnits','centimeters');
103
    set(hfig, 'PaperSize', [figwidth, figheight]);
104
    set(hfig, 'PaperPositionMode', 'manual');
105
    set(hfig, 'PaperPosition',[0 0 figwidth figheight]);    
106
    
107
    % reset to original units for axes and figure 
108
    if ~iscell(origaxunits)
109
        origaxunits = {origaxunits};
110
    end
111
112
    for i = 1:numel(hax)
113
        set(hax(i), 'Units', origaxunits{i});
114
    end
115
116
    set(hfig, 'Units', origfigunits);
117
    
118
%      set(hfig, 'WindowStyle', origwindowstyle);
119
     
120
end