2%> Alters the figure size corresponding to the input figure
3%> handle ``fig`` such that it has the minimum size necessary to
4%> enclose all axes in the figure without excess space around them.<br>
7%> This function expands the figure to completely encompass all axes
if necessary.<br>
8%> If any 3D axes are present
which have been zoomed, the procedure will produce an error,
9%> as these cannot easily be dealt with.<br>
11%> \param[in] fig : The input scalar MATLAB
object representing the
12%> handle to the figure whose axes must be tightly fit.<br>
13%> (**optional**. If missing, the current figure will be used.)
26%> This function build upon the work of,
27%> Richard Crozier (2024) [tightfig (hfig)](https:
28%> MATLAB Central File Exchange. Retrieved May 12, 2024.<br>
31%> \JoshuaOsborne, May 21 2024, 6:12 AM, University of Texas at Arlington<br>
32%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
33%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute
for Computational Engineering and Sciences (ICES), UT Austin<br>
43 %%%% There can be an issue with tightfig when the user has been modifying
44 %%%% the contents manually, the code below is an attempt to resolve
this,
45 %%%% but it has not yet been satisfactorily fixed
46 %%%% origwindowstyle = get(fig,
'WindowStyle');
48 set(fig,
'WindowStyle',
'normal');
50 %%%% 1 point is 0.3528 mm
for future use get all the axes handles
51 %%%% note
this will also fetch legends and color bars as well.
53 hax = findall(fig,
'type',
'axes');
55 % TODO: fix
for modern matlab, colorbars and legends are no longer axes.
57 hcbar = findall(fig,
'type',
'colorbar');
58 hleg = findall(fig,
'type',
'legend');
60 % get the original axes units, so we can change and reset these again later.
62 origaxunits = get(hax,
'Units');
64 % change the axes units to cm.
66 set(hax,
'Units',
'centimeters');
71 % get various position parameters of the axes.
74 % fsize = cell2mat(get(hax,
'FontSize'));
75 ti = cell2mat(get(hax,
'TightInset'));
76 pos = [pos; cell2mat(get(hax,
'Position')) ];
78 % fsize = get(hax,
'FontSize');
79 ti = get(hax,
'TightInset');
80 pos = [pos; get(hax,
'Position') ];
84 set(hcbar,
'Units',
'centimeters');
85 % colorbars
do not have tightinset
property
86 for cbind = 1:numel(hcbar)
87 % fsize = cell2mat(get(hax,
'FontSize'));
88 [cbarpos, cbarti] = colorbarpos (hcbar);
96 set(hleg,
'Units',
'centimeters');
98 % legends
do not have tightinset
property
100 % fsize = cell2mat(get(hax,
'FontSize'));
101 pos = [pos; cell2mat(get(hleg,
'Position')) ];
103 % fsize = get(hax,
'FontSize');
104 pos = [pos; get(hleg,
'Position') ];
106 ti = [ti; repmat([0,0,0,0], numel(hleg), 1); ];
109 % ensure very tiny border so outer box always appears.
113 % we will check
if any 3d axes are zoomed, to
do this we will
114 % check
if they are not being viewed in any of the 2d directions.
116 views2d = [0,90; 0,0; 90,0];
120 set(hax(i),
'LooseInset', ti(i,:));
121 % set(hax(i),
'LooseInset', [0,0,0,0]);
123 % get the current viewing angle of the axes
124 [az,el] = view(hax(i));
126 % determine
if the axes are zoomed
127 iszoomed = strcmp(get(hax(i),
'CameraViewAngleMode'),
'manual');
129 % test
if we are viewing in 2d mode or a 3d view
130 is2d = all(bsxfun(@eq, [az,el], views2d), 2);
132 if iszoomed && ~any(is2d)
133 error(
'fitAxes:haszoomed3d',
'Cannot make figures containing zoomed 3D axes tight.')
138 % we will move all the axes down and to the left by the amount
139 % necessary to just
show the bottom and leftmost axes and labels etc.
141 moveleft = min(pos(:,1) - ti(:,1));
142 movedown = min(pos(:,2) - ti(:,2));
144 % we will also alter the height and width of the figure to
145 % just encompass the topmost and rightmost axes and labels.
147 figwidth = max(pos(:,1) + pos(:,3) + ti(:,3) - moveleft);
148 figheight = max(pos(:,2) + pos(:,4) + ti(:,4) - movedown);
153 set(hax(i), 'Position', [pos(i,1:2) - [moveleft,movedown], pos(i,3:4)]);
156 for i = 1:numel(hcbar)
157 set(hcbar(i), 'Position', [pos(i+numel(hax),1:2) - [moveleft,movedown], pos(i+numel(hax),3:4)]);
160 for i = 1:numel(hleg)
161 set(hleg(i), 'Position', [pos(i+numel(hax)+numel(hcbar),1:2) - [moveleft,movedown], pos(i+numel(hax)+numel(hcbar),3:4)]);
164 origfigunits = get(fig, 'Units');
165 set(fig, 'Units', 'centimeters');
167 % change the size of the figure.
169 figpos = get(fig, 'Position');
171 set(fig, 'Position', [figpos(1), figpos(2), figwidth, figheight]);
173 % change the size of the paper.
175 set(fig, 'PaperUnits','centimeters');
176 set(fig, 'PaperSize', [figwidth, figheight]);
177 set(fig, 'PaperPositionMode', 'manual');
178 set(fig, 'PaperPosition',[0 0 figwidth figheight]);
180 % reset to original units for axes and figure.
182 if ~iscell(origaxunits)
183 origaxunits = {origaxunits};
186 set(hax(i),
'Units', origaxunits{i});
188 set(fig,
'Units', origfigunits);
190 % set(fig,
'WindowStyle', origwindowstyle);
195function [pos, ti] = colorbarpos(hcbar)
196 % 1 point is 0.3528 mm
197 pos = hcbar.Position;
199 if ~isempty (strfind (hcbar.Location,
'outside'))
200 if strcmp (hcbar.AxisLocation, 'out')
201 tlabels = hcbar.TickLabels;
202 fsize = hcbar.FontSize;
203 switch hcbar.Location
205 % make exta space a little more than the font size/height
206 ticklablespace_cm = 1.1 * (0.3528/10) * fsize;
207 ti(4) = ti(4) + ticklablespace_cm;
209 maxlabellen = max ( cellfun (@numel, tlabels, 'UniformOutput', true) );
210 % 0.62 factor is arbitrary and added because we don't
211 % know the width of every character in the label, the
212 % fsize refers to the height of the font
213 ticklablespace_cm = (0.3528/10) * fsize * maxlabellen * 0.62;
214 ti(3) = ti(3) + ticklablespace_cm;
216 % make exta space a little more than the font size/height
217 ticklablespace_cm = 1.1 * (0.3528/10) * fsize;
218 ti(2) = ti(2) + ticklablespace_cm;
220 maxlabellen = max ( cellfun (@numel, tlabels, 'UniformOutput', true) );
221 % 0.62 factor is arbitrary and added because we don't
222 % know the width of every character in the label, the
223 % fsize refers to the height of the font
224 ticklablespace_cm = (0.3528/10) * fsize * maxlabellen * 0.62;
225 ti(1) = ti(1) + ticklablespace_cm;
function fitAxes(in fig)
Alters the figure size corresponding to the input figure handle fig such that it has the minimum size...
function show(in obj, in name, in hidden)
Display the components of an input MATLAB variable on MATLAB Console recursively.
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...