time | calls | line |
---|
| | 1 | function theAxis = subplot(nRows, nCols, plotId, varargin)
|
| | 2 | %SUBPLOT Create axes in tiled positions.
|
| | 3 | % H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window
|
| | 4 | % into an m-by-n matrix of small axes, selects the p-th axes for
|
| | 5 | % the current plot, and returns the axes handle. The axes are
|
| | 6 | % counted along the top row of the Figure window, then the second
|
| | 7 | % row, etc. For example,
|
| | 8 | %
|
| | 9 | % SUBPLOT(2,1,1), PLOT(income)
|
| | 10 | % SUBPLOT(2,1,2), PLOT(outgo)
|
| | 11 | %
|
| | 12 | % plots income on the top half of the window and outgo on the
|
| | 13 | % bottom half. If the CurrentAxes is nested in a uipanel the
|
| | 14 | % panel is used as the parent for the subplot instead of the
|
| | 15 | % current figure.
|
| | 16 | %
|
| | 17 | % SUBPLOT(m,n,p), if the axes already exists, makes it current.
|
| | 18 | % SUBPLOT(m,n,p,'replace'), if the axes already exists, deletes it and
|
| | 19 | % creates a new axes.
|
| | 20 | % SUBPLOT(m,n,p,'align') places the axes so that the plot boxes
|
| | 21 | % are aligned, but does not prevent the labels and ticks from
|
| | 22 | % overlapping.
|
| | 23 | % SUBPLOT(m,n,P), where P is a vector, specifies an axes position
|
| | 24 | % that covers all the subplot positions listed in P.
|
| | 25 | % SUBPLOT(H), where H is an axes handle, is another way of making
|
| | 26 | % an axes current for subsequent plotting commands.
|
| | 27 | %
|
| | 28 | % SUBPLOT('position',[left bottom width height]) creates an
|
| | 29 | % axes at the specified position in normalized coordinates (in
|
| | 30 | % in the range from 0.0 to 1.0).
|
| | 31 | %
|
| | 32 | % SUBPLOT(..., PROP1, VALUE1, PROP2, VALUE2, ...) sets the
|
| | 33 | % specified property-value pairs on the subplot axes. To add the
|
| | 34 | % subplot to a specific figure pass the figure handle as the
|
| | 35 | % value for the 'Parent' property.
|
| | 36 | %
|
| | 37 | % If a SUBPLOT specification causes a new axes to overlap an
|
| | 38 | % existing axes, the existing axes is deleted - unless the position
|
| | 39 | % of the new and existing axes are identical. For example,
|
| | 40 | % the statement SUBPLOT(1,2,1) deletes all existing axes overlapping
|
| | 41 | % the left side of the Figure window and creates a new axes on that
|
| | 42 | % side - unless there is an axes there with a position that exactly
|
| | 43 | % matches the position of the new axes (and 'replace' was not specified),
|
| | 44 | % in which case all other overlapping axes will be deleted and the
|
| | 45 | % matching axes will become the current axes.
|
| | 46 | %
|
| | 47 | % SUBPLOT(111) is an exception to the rules above, and is not
|
| | 48 | % identical in behavior to SUBPLOT(1,1,1). For reasons of backwards
|
| | 49 | % compatibility, it is a special case of subplot which does not
|
| | 50 | % immediately create an axes, but instead sets up the figure so that
|
| | 51 | % the next graphics command executes CLF RESET in the figure
|
| | 52 | % (deleting all children of the figure), and creates a new axes in
|
| | 53 | % the default position. This syntax does not return a handle, so it
|
| | 54 | % is an error to specify a return argument. The delayed CLF RESET
|
| | 55 | % is accomplished by setting the figure's NextPlot to 'replace'.
|
| | 56 | %
|
| | 57 | % Be aware when creating subplots from scripts that the Position
|
| | 58 | % property of subplots is not finalized until either a drawnow
|
| | 59 | % command is issued, or MATLAB returns to await a user command.
|
| | 60 | % That is, the value obtained for subplot i by the command
|
| | 61 | % h(i).Position will not be correct until the script
|
| | 62 | % refreshes the plot or exits.
|
| | 63 | %
|
| | 64 | % See also GCA, GCF, AXES, FIGURE, UIPANEL
|
| | 65 |
|
| | 66 | % SUBPLOT(m,n,p,H) when H is an axes will move H to the specified
|
| | 67 | % position.
|
| | 68 | % SUBPLOT(m,n,p,H,PROP1,VALUE1,...) will move H and apply the
|
| | 69 | % specified property-value pairs
|
| | 70 | %
|
| | 71 | % SUBPLOT(m,n,p) for non-integer p places the subplot at the
|
| | 72 | % fraction p-floor(p) between the positions floor(p) and ceil(p)
|
| | 73 |
|
| | 74 | % Copyright 1984-2014 The MathWorks, Inc.
|
| | 75 |
|
| 44 | 76 | narg = nargin;
|
| | 77 |
|
| | 78 | % First we check whether Handle Graphics uses MATLAB classes
|
| 44 | 79 | isHGUsingMATLABClasses = ishg2parent(varargin{:});
|
| | 80 |
|
| 44 | 81 | if isHGUsingMATLABClasses
|
| 44 | 82 | try
|
| 44 | 83 | if nargout == 0
|
| 36 | 84 | if narg == 0
|
| | 85 | subplotHGUsingMATLABClasses();
|
| 36 | 86 | elseif narg == 1
|
| | 87 | subplotHGUsingMATLABClasses(nRows);
|
| 36 | 88 | elseif narg == 2
|
0.21 | 36 | 89 | subplotHGUsingMATLABClasses(nRows, nCols);
|
| | 90 | elseif narg == 3
|
| | 91 | subplotHGUsingMATLABClasses(nRows, nCols, plotId);
|
| | 92 | else
|
| | 93 | subplotHGUsingMATLABClasses(nRows, nCols, plotId, varargin{:});
|
| | 94 | end
|
| 8 | 95 | else
|
| 8 | 96 | if narg == 0
|
| | 97 | theAxis = subplotHGUsingMATLABClasses();
|
| 8 | 98 | elseif narg == 1
|
| | 99 | theAxis = subplotHGUsingMATLABClasses(nRows);
|
| 8 | 100 | elseif narg == 2
|
0.03 | 8 | 101 | theAxis = subplotHGUsingMATLABClasses(nRows, nCols);
|
| | 102 | elseif narg == 3
|
| | 103 | theAxis = subplotHGUsingMATLABClasses(nRows, nCols, plotId);
|
| | 104 | else
|
| | 105 | theAxis = subplotHGUsingMATLABClasses(nRows, nCols, plotId, varargin{:});
|
| | 106 | end
|
| 8 | 107 | end
|
| | 108 | catch me
|
| | 109 | throw(me)
|
| | 110 | end
|
| | 111 | else
|
| | 112 | % we will kill all overlapping axes siblings if we encounter the mnp
|
| | 113 | % or m,n,p specifier (excluding '111').
|
| | 114 | % But if we get the 'position' or H specifier, we won't check for and
|
| | 115 | % delete overlapping siblings:
|
| | 116 | killSiblings = 0;
|
| | 117 | createAxis = true;
|
| | 118 | moveAxis = false;
|
| | 119 | delayDestroy = false;
|
| | 120 | useAutoLayout = true;
|
| | 121 | tol = sqrt(eps);
|
| | 122 | parent = get(0, 'CurrentFigure');
|
| | 123 | ancestorFigure = parent;
|
| | 124 | if ~isempty(parent) && ~isempty(get(parent, 'CurrentAxes'))
|
| | 125 | parent = get(get(parent, 'CurrentAxes'), 'Parent');
|
| | 126 | ancestorFigure = parent;
|
| | 127 | if ~strcmp(get(ancestorFigure, 'Type'), 'figure')
|
| | 128 | ancestorFigure = ancestor(parent, 'figure');
|
| | 129 | end
|
| | 130 | end
|
| | 131 | pvpairs = {};
|
| | 132 | preventMove = false;
|
| | 133 | % This is the percent offset from the subplot grid of the plotbox.
|
| | 134 | inset = [.2, .18, .04, .1]; % [left bottom right top]
|
| | 135 |
|
| | 136 | %check for encoded format
|
| | 137 | h = [];
|
| | 138 | position = [];
|
| | 139 | explicitParent = false;
|
| | 140 | explicitPosition = false;
|
| | 141 |
|
| | 142 | if narg == 0 % make compatible with 3.5, i.e. subplot == subplot(111)
|
| | 143 | nRows = 111;
|
| | 144 | narg = 1;
|
| | 145 | end
|
| | 146 |
|
| | 147 | if narg == 1
|
| | 148 | % The argument could be one of 3 things:
|
| | 149 | % 1) a 3-digit number 100 < num < 1000, of the format mnp
|
| | 150 | % 2) a 3-character string containing a number as above
|
| | 151 | % 3) an axes handle
|
| | 152 | arg = nRows;
|
| | 153 |
|
| | 154 | % turn string into a number:
|
| | 155 | if(ischar(arg))
|
| | 156 | arg = str2double(arg);
|
| | 157 | end
|
| | 158 |
|
| | 159 | % Check for NaN and Inf.
|
| | 160 | if (~isfinite(arg))
|
| | 161 | error(message('MATLAB:subplot:SubplotIndexNonFinite'))
|
| | 162 | end
|
| | 163 |
|
| | 164 | % number with a fractional part can only be an identifier:
|
| | 165 | if(rem(arg, 1) > 0)
|
| | 166 | h = arg;
|
| | 167 | if ~ishghandle(h, 'axes')
|
| | 168 | error(message('MATLAB:subplot:InvalidAxesHandle'))
|
| | 169 | end
|
| | 170 | createAxis = false;
|
| | 171 | % all other numbers will be converted to mnp format:
|
| | 172 | else
|
| | 173 | % Check for input out of range
|
| | 174 | if (arg <= 100 || arg >= 1000)
|
| | 175 | error(message('MATLAB:subplot:SubplotIndexOutOfRange'))
|
| | 176 | end
|
| | 177 |
|
| | 178 | plotId = rem(arg, 10);
|
| | 179 | nCols = rem(fix(arg - plotId) / 10, 10);
|
| | 180 | nRows = fix(arg / 100);
|
| | 181 | if nRows * nCols < plotId
|
| | 182 | error(message('MATLAB:subplot:SubplotIndexTooLarge'));
|
| | 183 | end
|
| | 184 | killSiblings = 1;
|
| | 185 | if (arg == 111)
|
| | 186 | createAxis = false;
|
| | 187 | delayDestroy = true;
|
| | 188 | else
|
| | 189 | createAxis = true;
|
| | 190 | delayDestroy = false;
|
| | 191 | end
|
| | 192 | end
|
| | 193 |
|
| | 194 | elseif narg == 2
|
| | 195 | % The arguments MUST be the string 'position' and a 4-element vector:
|
| | 196 | if (strcmpi(nRows, 'position'))
|
| | 197 | pos_size = size(nCols);
|
| | 198 | if (pos_size(1) * pos_size(2) == 4)
|
| | 199 | position = nCols;
|
| | 200 | explicitPosition = true;
|
| | 201 | else
|
| | 202 | error(message('MATLAB:subplot:InvalidPositionParameter'))
|
| | 203 | end
|
| | 204 | else
|
| | 205 | error(message('MATLAB:subplot:UnknownOption'))
|
| | 206 | end
|
| | 207 | killSiblings = 1; % Kill overlaps here also.
|
| | 208 | useAutoLayout = false;
|
| | 209 |
|
| | 210 | elseif narg == 3
|
| | 211 | % passed in subplot(m,n,p) -- we should kill overlaps
|
| | 212 | % here too:
|
| | 213 | killSiblings = 1;
|
| | 214 |
|
| | 215 | elseif narg >= 4
|
| | 216 | if ~ischar(nRows)
|
| | 217 | arg = varargin{1};
|
| | 218 | if ~ischar(arg)
|
| | 219 | % passed in subplot(m,n,p,H,...)
|
| | 220 | h = arg;
|
| | 221 | if ~ishghandle(h, 'axes') || ...
|
| | 222 | isa(handle(h), 'scribe.colorbar') || ...
|
| | 223 | isa(handle(h), 'scribe.legend')
|
| | 224 | error(message('MATLAB:subplot:InvalidAxesHandle'))
|
| | 225 | end
|
| | 226 | parent = get(h, 'Parent');
|
| | 227 | ancestorFigure = ancestor(h, 'figure');
|
| | 228 | % If the parent is passed in explicitly, don't create a new figure
|
| | 229 | % when the "NextPlot" property is set to "new" in the figure.
|
| | 230 | explicitParent = true;
|
| | 231 | set(ancestorFigure, 'CurrentAxes', h);
|
| | 232 | moveAxis = true;
|
| | 233 | createAxis = false;
|
| | 234 | if narg >= 5 && strcmpi(varargin{2}, 'PreventMove')
|
| | 235 | preventMove = true;
|
| | 236 | pvpairs = varargin(3 : end);
|
| | 237 | else
|
| | 238 | pvpairs = varargin(2 : end);
|
| | 239 | end
|
| | 240 | elseif strncmpi(arg, 'replace', 1)
|
| | 241 | % passed in subplot(m,n,p,'replace')
|
| | 242 | killSiblings = 2; % kill nomatter what
|
| | 243 | elseif strcmpi(arg, 'align')
|
| | 244 | % passed in subplot(m,n,p,'align')
|
| | 245 | % since obeying position will remove the axes from the grid just set
|
| | 246 | % useAutoLayout to false to skip adding it to the grid to start with
|
| | 247 | useAutoLayout = false;
|
| | 248 | killSiblings = 1; % kill if it overlaps stuff
|
| | 249 | elseif strcmpi(arg, 'v6')
|
| | 250 | % passed in subplot(m,n,p,'v6')
|
| | 251 | % since obeying position will remove the axes from the grid just set
|
| | 252 | % useAutoLayout to false to skip adding it to the grid to start with
|
| | 253 | warning(['MATLAB:', mfilename, ':DeprecatedV6Argument'],...
|
| | 254 | getString(message('MATLAB:usev6plotapi:DeprecatedV6ArgumentForFilename', upper(mfilename))));
|
| | 255 | useAutoLayout = false;
|
| | 256 | killSiblings = 1; % kill if it overlaps stuff
|
| | 257 | else
|
| | 258 | % passed in prop-value pairs
|
| | 259 | killSiblings = 1;
|
| | 260 | pvpairs = varargin;
|
| | 261 | par = find(strncmpi('Parent', pvpairs(1 : 2 : end), 6));
|
| | 262 | if any(par)
|
| | 263 | % If the parent is passed in explicitly, don't create a new figure
|
| | 264 | % when the "NextPlot" property is set to "new" in the figure.
|
| | 265 | explicitParent = true;
|
| | 266 | parent = varargin{2 * par(1)};
|
| | 267 | ancestorFigure = ancestor(parent, 'figure');
|
| | 268 | end
|
| | 269 | end
|
| | 270 | else
|
| | 271 | % Passed in "Position" syntax with P/V pairs
|
| | 272 | % The arguments MUST be the string 'position' and a 4-element vector:
|
| | 273 | if (strcmpi(nRows, 'position'))
|
| | 274 | pos_size = size(nCols);
|
| | 275 | if (pos_size(1) * pos_size(2) == 4)
|
| | 276 | position = nCols;
|
| | 277 | explicitPosition = true;
|
| | 278 | else
|
| | 279 | error(message('MATLAB:subplot:InvalidPositionParameter'))
|
| | 280 | end
|
| | 281 | else
|
| | 282 | error(message('MATLAB:subplot:UnknownOption'))
|
| | 283 | end
|
| | 284 | killSiblings = 1; % Kill overlaps here also.
|
| | 285 | useAutoLayout = false;
|
| | 286 | pvpairs = [{plotId}, varargin];
|
| | 287 | par = find(strncmpi('Parent', pvpairs(1 : 2 : end), 6));
|
| | 288 | if any(par)
|
| | 289 | % If the parent is passed in explicitly, don't create a new figure
|
| | 290 | % when the "NextPlot" property is set to "new" in the figure.
|
| | 291 | explicitParent = true;
|
| | 292 | parent = pvpairs{2 * par(1)};
|
| | 293 | ancestorFigure = ancestor(parent, 'figure');
|
| | 294 | end
|
| | 295 | end
|
| | 296 | end
|
| | 297 |
|
| | 298 | % if we recovered an identifier earlier, use it:
|
| | 299 | if ~isempty(h) && ~moveAxis
|
| | 300 | parent = get(h, 'Parent');
|
| | 301 | ancestorFigure = ancestor(h, 'figure');
|
| | 302 | set(ancestorFigure, 'CurrentAxes', h);
|
| | 303 | else % if we haven't recovered position yet, generate it from mnp info:
|
| | 304 | if isempty(parent)
|
| | 305 | parent = gcf;
|
| | 306 | ancestorFigure = parent;
|
| | 307 | end
|
| | 308 | if isempty(position)
|
| | 309 | if min(plotId) < 1
|
| | 310 | error(message('MATLAB:subplot:SubplotIndexTooSmall'))
|
| | 311 | elseif max(plotId) > nCols * nRows
|
| | 312 | error(message('MATLAB:subplot:SubplotIndexTooLarge'));
|
| | 313 | else
|
| | 314 |
|
| | 315 | row = (nRows - 1) - fix((plotId - 1) / nCols);
|
| | 316 | col = rem(plotId - 1, nCols);
|
| | 317 |
|
| | 318 | % get default axes position in normalized units
|
| | 319 | % If we have checked this quanitity once, cache it.
|
| | 320 | if ~isappdata(ancestorFigure, 'SubplotDefaultAxesLocation')
|
| | 321 | if ~strcmp(get(ancestorFigure, 'DefaultAxesUnits'), 'normalized')
|
| | 322 | tmp = axes;
|
| | 323 | set(tmp, 'Units', 'normalized')
|
| | 324 | def_pos = get(tmp, 'Position');
|
| | 325 | delete(tmp)
|
| | 326 | else
|
| | 327 | def_pos = get(ancestorFigure, 'DefaultAxesPosition');
|
| | 328 | end
|
| | 329 | setappdata(ancestorFigure, 'SubplotDefaultAxesLocation', def_pos);
|
| | 330 | else
|
| | 331 | def_pos = getappdata(ancestorFigure, 'SubplotDefaultAxesLocation');
|
| | 332 | end
|
| | 333 |
|
| | 334 | % compute outerposition and insets relative to figure bounds
|
| | 335 | rw = max(row) - min(row) + 1;
|
| | 336 | cw = max(col) - min(col) + 1;
|
| | 337 | width = def_pos(3) / (nCols - inset(1) - inset(3));
|
| | 338 | height = def_pos(4) / (nRows - inset(2) - inset(4));
|
| | 339 | inset = inset .* [width, height, width, height];
|
| | 340 | outerpos = [def_pos(1) + min(col) * width - inset(1), ...
|
| | 341 | def_pos(2) + min(row) * height - inset(2), ...
|
| | 342 | width * cw, height * rw];
|
| | 343 |
|
| | 344 | % adjust outerpos and insets for axes around the outside edges
|
| | 345 | if min(col) == 0
|
| | 346 | inset(1) = def_pos(1);
|
| | 347 | outerpos(3) = outerpos(1) + outerpos(3);
|
| | 348 | outerpos(1) = 0;
|
| | 349 | end
|
| | 350 | if min(row) == 0
|
| | 351 | inset(2) = def_pos(2);
|
| | 352 | outerpos(4) = outerpos(2) + outerpos(4);
|
| | 353 | outerpos(2) = 0;
|
| | 354 | end
|
| | 355 | if max(col) == nCols - 1
|
| | 356 | inset(3) = max(0, 1 - def_pos(1) - def_pos(3));
|
| | 357 | outerpos(3) = 1 - outerpos(1);
|
| | 358 | end
|
| | 359 | if max(row) == nRows - 1
|
| | 360 | inset(4) = max(0, 1 - def_pos(2) - def_pos(4));
|
| | 361 | outerpos(4) = 1 - outerpos(2);
|
| | 362 | end
|
| | 363 |
|
| | 364 | % compute inner position
|
| | 365 | position = [outerpos(1 : 2) + inset(1 : 2), ...
|
| | 366 | outerpos(3 : 4) - inset(1 : 2) - inset(3 : 4)];
|
| | 367 |
|
| | 368 | end
|
| | 369 | end
|
| | 370 | end
|
| | 371 |
|
| | 372 | % kill overlapping siblings if mnp specifier was used:
|
| | 373 | nextstate = get(ancestorFigure, 'NextPlot');
|
| | 374 |
|
| | 375 | if strncmp(nextstate, 'replace', 7)
|
| | 376 | nextstate = 'add';
|
| | 377 | elseif strncmp(nextstate, 'new', 3)
|
| | 378 | killSiblings = 0;
|
| | 379 | end
|
| | 380 |
|
| | 381 | if killSiblings
|
| | 382 | if delayDestroy
|
| | 383 | if nargout
|
| | 384 | error(message('MATLAB:subplot:TooManyOutputs'))
|
| | 385 | else
|
| | 386 | set(ancestorFigure, 'NextPlot', 'replace');
|
| | 387 | return
|
| | 388 | end
|
| | 389 | end
|
| | 390 | sibs = datachildren(parent);
|
| | 391 | newcurrent = [];
|
| | 392 | for i = 1 : length(sibs)
|
| | 393 | % Be aware that handles in this list might be destroyed before
|
| | 394 | % we get to them, because of other objects' DeleteFcn callbacks...
|
| | 395 | if ishghandle(sibs(i), 'axes')
|
| | 396 | units = get(sibs(i), 'Units');
|
| | 397 | sibpos = get(sibs(i), 'Position');
|
| | 398 | % If a legend or colorbar has resized the axes, use the original axes
|
| | 399 | % position as the "Position" property:
|
| | 400 | if ~explicitPosition
|
| | 401 | if isappdata(sibs(i), 'LegendColorbarExpectedPosition') && ...
|
| | 402 | isequal(getappdata(sibs(i), 'LegendColorbarExpectedPosition'), get(sibs(i), 'Position'))
|
| | 403 | sibinset = getappdata(sibs(i), 'LegendColorbarOriginalInset');
|
| | 404 | if isempty(sibinset)
|
| | 405 | % during load the appdata might not be present
|
| | 406 | sibinset = get(get(sibs(i), 'Parent'), 'DefaultAxesLooseInset');
|
| | 407 | end
|
| | 408 | sibinset = offsetsInUnits(sibs(i), sibinset, 'normalized', get(sibs(i), 'Units'));
|
| | 409 | if strcmpi(get(sibs(i), 'ActivePositionProperty'), 'position')
|
| | 410 | pos = get(sibs(i), 'Position');
|
| | 411 | loose = get(sibs(i), 'LooseInset');
|
| | 412 | opos = getOuterFromPosAndLoose(pos, loose, get(sibs(i), 'Units'));
|
| | 413 | if strcmp(get(sibs(i), 'Units'), 'normalized')
|
| | 414 | sibinset = [opos(3 : 4), opos(3 : 4)] .* sibinset;
|
| | 415 | end
|
| | 416 | sibpos = [opos(1 : 2) + sibinset(1 : 2), opos(3 : 4) - sibinset(1 : 2) - sibinset(3 : 4)];
|
| | 417 | end
|
| | 418 | end
|
| | 419 | end
|
| | 420 | if ~strcmp(units, 'normalized')
|
| | 421 | sibpos = hgconvertunits(ancestorFigure, sibpos, units, 'normalized', parent);
|
| | 422 | end
|
| | 423 | intersect = 1;
|
| | 424 | if ((position(1) >= sibpos(1) + sibpos(3) - tol) || ...
|
| | 425 | (sibpos(1) >= position(1) + position(3) - tol) || ...
|
| | 426 | (position(2) >= sibpos(2) + sibpos(4) - tol) || ...
|
| | 427 | (sibpos(2) >= position(2) + position(4) - tol))
|
| | 428 | intersect = 0;
|
| | 429 | end
|
| | 430 | if intersect
|
| | 431 | % position is the proposed position of an axes, and
|
| | 432 | % sibpos is the current position of an existing axes.
|
| | 433 | % Since the bounding boxes of position and sibpos overlap,
|
| | 434 | % we must determine whether to delete the sibling sibs(i)
|
| | 435 | % whose normalized position is sibpos.
|
| | 436 |
|
| | 437 | % First of all, we check whether we must kill the sibling
|
| | 438 | % "no matter what."
|
| | 439 | if (killSiblings == 2)
|
| | 440 | delete(sibs(i));
|
| | 441 |
|
| | 442 | % If the proposed and existing axes overlap exactly, we do
|
| | 443 | % not kill the sibling. Rather we shall ensure later that
|
| | 444 | % this sibling axes is set as the 'CurrentAxes' of its
|
| | 445 | % ancestorFigure.
|
| | 446 |
|
| | 447 | % Next we check for a partial overlap.
|
| | 448 | elseif (any(abs(sibpos - position) > tol))
|
| | 449 | % The proposed and existing axes partially overlap.
|
| | 450 | % Since the proposed and existing axes could each be
|
| | 451 | % "grid-generated" or "explicitly-specified", we must
|
| | 452 | % consider four possibilities for the overlap of
|
| | 453 | % "proposed" vs. "existing", i.e.
|
| | 454 | % (1) "grid-generated" vs. "grid-generated"
|
| | 455 | % (2) "grid-generated" vs. "explicitly-specified"
|
| | 456 | % (3) "explicitly-specified" vs. "grid-generated"
|
| | 457 | % (4) "explicitly-specified" vs. "explicitly-specified"
|
| | 458 |
|
| | 459 | % If the position of the proposed axes is
|
| | 460 | % "explicitly-specified", then the only condition that
|
| | 461 | % avoids killing the sibling is an exact overlap.
|
| | 462 | % However, we know that the overlap is partial.
|
| | 463 | if (explicitPosition)
|
| | 464 | delete(sibs(i));
|
| | 465 | else
|
| | 466 | % We know that the position of the proposed axes is
|
| | 467 | % "grid-generated".
|
| | 468 |
|
| | 469 | grid = getappdata(parent, 'SubplotGrid');
|
| | 470 | % The SubplotGrid maintains an array of axes
|
| | 471 | % handles, one per grid location. Axes that span
|
| | 472 | % multiple grid locations do not store handles in
|
| | 473 | % the SubplotGrid.
|
| | 474 |
|
| | 475 | if isempty(grid) || ~any(grid(:) == sibs(i)) || ...
|
| | 476 | size(grid, 1) ~= nRows || size(grid, 2) ~= nCols || ...
|
| | 477 | ~isscalar(row) || ~isscalar(col)
|
| | 478 | % If the sibling cannot be found in the grid, we
|
| | 479 | % kill the sibling. Otherwise, the proposed and
|
| | 480 | % existing axes are "grid-generated". If we
|
| | 481 | % are changing the size of the grid, we kill
|
| | 482 | % the sibling. Otherwise, "plotId" may be a
|
| | 483 | % vector of multiple grid locations, which
|
| | 484 | % causes a partial overlap between the proposed
|
| | 485 | % and existing axes, so we kill the sibling.
|
| | 486 |
|
| | 487 | % This check recognizes that there may be
|
| | 488 | % labels, colorbars, legends, etc. attached to
|
| | 489 | % the existing axes that have affected its
|
| | 490 | % position. In such a case, we do not kill the
|
| | 491 | % sibling.
|
| | 492 | delete(sibs(i));
|
| | 493 | end
|
| | 494 | end
|
| | 495 | end
|
| | 496 | if ishghandle(sibs(i))
|
| | 497 | % if this axes overlaps the other one exactly then
|
| | 498 | if ~isempty(newcurrent) && ishghandle(newcurrent)
|
| | 499 | delete(newcurrent);
|
| | 500 | end
|
| | 501 | newcurrent = sibs(i);
|
| | 502 | end
|
| | 503 | end
|
| | 504 | end
|
| | 505 | end
|
| | 506 | if ~isempty(newcurrent) && ishghandle(newcurrent)
|
| | 507 | set(ancestorFigure, 'CurrentAxes', newcurrent);
|
| | 508 | createAxis = false;
|
| | 509 | end
|
| | 510 | set(ancestorFigure, 'NextPlot', nextstate);
|
| | 511 | end
|
| | 512 |
|
| | 513 | % create the axes:
|
| | 514 | if createAxis
|
| | 515 | if strcmp(nextstate, 'new') && ~explicitParent
|
| | 516 | parent = figure;
|
| | 517 | ancestorFigure = parent;
|
| | 518 | end
|
| | 519 | ax = axes('Units', 'normalized', 'Position', position, ...
|
| | 520 | 'LooseInset', inset, 'Parent', parent);
|
| | 521 | % TODO: Get axes to accept position args on command line
|
| | 522 | set(ax, 'Units', get(double(ancestorFigure), 'DefaultAxesUnits'))
|
| | 523 | if useAutoLayout
|
| | 524 | addAxesToGrid(ax, nRows, nCols, row, col, position);
|
| | 525 | end
|
| | 526 | if ~isempty(pvpairs)
|
| | 527 | set(ax, pvpairs{:});
|
| | 528 | end
|
| | 529 | elseif moveAxis && ~preventMove
|
| | 530 | ax = h;
|
| | 531 | units = get(h, 'Units');
|
| | 532 | set(h, 'Units', 'normalized', 'Position', position, ...
|
| | 533 | 'LooseInset', inset, 'Parent', parent);
|
| | 534 | set(h, 'Units', units);
|
| | 535 | if useAutoLayout
|
| | 536 | addAxesToGrid(ax, nRows, nCols, row, col, position);
|
| | 537 | end
|
| | 538 | if ~isempty(pvpairs)
|
| | 539 | set(h, pvpairs{:});
|
| | 540 | end
|
| | 541 | else
|
| | 542 | % this should only happen with subplot(H)
|
| | 543 | ax = get(ancestorFigure, 'CurrentAxes');
|
| | 544 | end
|
| | 545 |
|
| | 546 | % return identifier, if requested:
|
| | 547 | if(nargout > 0)
|
| | 548 | theAxis = ax;
|
| | 549 | end
|
| | 550 | end
|
| 44 | 551 | end
|
Other subfunctions in this file are not included in this listing.