This is a static copy of a profile reportHome
subplot (249 calls, 1.705 sec)
Generated 05-Aug-2011 13:01:35 using cpu time.
function in file /usr/local/MATLAB/R2011a/toolbox/matlab/graph2d/subplot.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
377 | sibpos = get(sibs(i), 'Positio... | 1400 | 0.525 s | 30.8% |  |
504 | addAxesToGrid(ax, nRows, nCols... | 159 | 0.284 s | 16.7% |  |
487 | set(ancestorFigure, 'CurrentAx... | 90 | 0.273 s | 16.0% |  |
499 | ax = axes('Units', 'normalized... | 159 | 0.197 s | 11.5% |  |
370 | sibs = datachildren(parent); | 249 | 0.087 s | 5.1% |  |
All other lines | | | 0.339 s | 19.9% |  |
Totals | | | 1.705 s | 100% | |
Children (called functions)
Code Analyzer results
No Code Analyzer messages.Coverage results
[ Show coverage for parent directory ]
Total lines in function | 531 |
Non-code lines (comments, blank lines) | 219 |
Code lines (lines that can run) | 312 |
Code lines that did run | 132 |
Code lines that did not run | 180 |
Coverage (did run/can run) | 42.31 % |
Function listing
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 % get(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-2010 The MathWorks, Inc.
75
249 76 narg = nargin;
77
78 % First we check whether Handle Graphics uses MATLAB classes
249 79 isHGUsingMATLABClasses = feature('HGUsingMATLABClasses');
80
249 81 if isHGUsingMATLABClasses
82 if nargout == 0
83 if narg == 0
84 subplotHGUsingMATLABClasses();
85 elseif narg == 1
86 subplotHGUsingMATLABClasses(nRows);
87 elseif narg == 2
88 subplotHGUsingMATLABClasses(nRows, nCols);
89 elseif narg == 3
90 subplotHGUsingMATLABClasses(nRows, nCols, plotId);
91 else
92 subplotHGUsingMATLABClasses(nRows, nCols, plotId, varargin{:});
93 end
94 else
95 if narg == 0
96 theAxis = subplotHGUsingMATLABClasses();
97 elseif narg == 1
98 theAxis = subplotHGUsingMATLABClasses(nRows);
99 elseif narg == 2
100 theAxis = subplotHGUsingMATLABClasses(nRows, nCols);
101 elseif narg == 3
102 theAxis = subplotHGUsingMATLABClasses(nRows, nCols, plotId);
103 else
104 theAxis = subplotHGUsingMATLABClasses(nRows, nCols, plotId, varargin{:});
105 end
106 end
249 107 else
108 % we will kill all overlapping axes siblings if we encounter the mnp
109 % or m,n,p specifier (excluding '111').
110 % But if we get the 'position' or H specifier, we won't check for and
111 % delete overlapping siblings:
249 112 killSiblings = 0;
249 113 createAxis = true;
249 114 moveAxis = false;
249 115 delayDestroy = false;
249 116 useAutoLayout = true;
249 117 tol = sqrt(eps);
0.02 249 118 parent = get(0, 'CurrentFigure');
249 119 ancestorFigure = parent;
0.01 249 120 if ~isempty(parent) && ~isempty(get(parent, 'CurrentAxes'))
229 121 parent = get(get(parent, 'CurrentAxes'), 'Parent');
229 122 ancestorFigure = parent;
229 123 if ~strcmp(get(ancestorFigure, 'Type'), 'figure')
124 ancestorFigure = ancestor(parent, 'figure');
125 end
229 126 end
249 127 pvpairs = {};
249 128 preventMove = false;
129 % This is the percent offset from the subplot grid of the plotbox.
249 130 inset = [.2, .18, .04, .1]; % [left bottom right top]
131
132 %check for encoded format
249 133 h = [];
249 134 position = [];
0.01 249 135 explicitParent = false;
249 136 explicitPosition = false;
137
249 138 if narg == 0 % make compatible with 3.5, i.e. subplot == subplot(111)
139 nRows = 111;
140 narg = 1;
141 end
142
249 143 if narg == 1
144 % The argument could be one of 3 things:
145 % 1) a 3-digit number 100 < num < 1000, of the format mnp
146 % 2) a 3-character string containing a number as above
147 % 3) an axes handle
148 arg = nRows;
149
150 % turn string into a number:
151 if(ischar(arg))
152 arg = str2double(arg);
153 end
154
155 % Check for NaN and Inf.
156 if (~isfinite(arg))
157 error(id('SubplotIndexNonFinite'), 'Index must be a finite 3-digit number of the format mnp.')
158 end
159
160 % number with a fractional part can only be an identifier:
161 if(rem(arg, 1) > 0)
162 h = arg;
163 if ~ishghandle(h, 'axes')
164 error(id('InvalidAxesHandle'), 'Requires valid axes handle for input.')
165 end
166 createAxis = false;
167 % all other numbers will be converted to mnp format:
168 else
169 % Check for input out of range
170 if (arg <= 100 || arg >= 1000)
171 error(id('SubplotIndexOutOfRange'), 'Index must be a 3-digit number of the format mnp.')
172 end
173
174 plotId = rem(arg, 10);
175 nCols = rem(fix(arg - plotId) / 10, 10);
176 nRows = fix(arg / 100);
177 if nRows * nCols < plotId
178 error(id('SubplotIndexTooLarge'), 'Index exceeds number of subplots.');
179 end
180 killSiblings = 1;
181 if (arg == 111)
182 createAxis = false;
183 delayDestroy = true;
184 else
185 createAxis = true;
186 delayDestroy = false;
187 end
188 end
189
249 190 elseif narg == 2
191 % The arguments MUST be the string 'position' and a 4-element vector:
192 if (strcmpi(nRows, 'position'))
193 pos_size = size(nCols);
194 if (pos_size(1) * pos_size(2) == 4)
195 position = nCols;
196 explicitPosition = true;
197 else
198 error(id('InvalidPositionParameter'), ...
199 'Position must be of the form [left bottom width height].')
200 end
201 else
202 error(id('UnknownOption'), 'Unknown command option.')
203 end
204 killSiblings = 1; % Kill overlaps here also.
205 useAutoLayout = false;
206
249 207 elseif narg == 3
208 % passed in subplot(m,n,p) -- we should kill overlaps
209 % here too:
0.01 249 210 killSiblings = 1;
211
212 elseif narg >= 4
213 if ~ischar(nRows)
214 arg = varargin{1};
215 if ~ischar(arg)
216 % passed in subplot(m,n,p,H,...)
217 h = arg;
218 if ~ishghandle(h, 'axes') || ...
219 isa(handle(h), 'scribe.colorbar') || ...
220 isa(handle(h), 'scribe.legend')
221 error(id('InvalidAxesHandle'), 'Requires valid axes handle for input.')
222 end
223 parent = get(h, 'Parent');
224 ancestorFigure = ancestor(h, 'figure');
225 % If the parent is passed in explicitly, don't create a new figure
226 % when the "NextPlot" property is set to "new" in the figure.
227 explicitParent = true;
228 set(ancestorFigure, 'CurrentAxes', h);
229 moveAxis = true;
230 createAxis = false;
231 if narg >= 5 && strcmpi(varargin{2}, 'PreventMove')
232 preventMove = true;
233 pvpairs = varargin(3 : end);
234 else
235 pvpairs = varargin(2 : end);
236 end
237 elseif strncmpi(arg, 'replace', 1)
238 % passed in subplot(m,n,p,'replace')
239 killSiblings = 2; % kill nomatter what
240 elseif strcmpi(arg, 'align')
241 % passed in subplot(m,n,p,'align')
242 % since obeying position will remove the axes from the grid just set
243 % useAutoLayout to false to skip adding it to the grid to start with
244 useAutoLayout = false;
245 killSiblings = 1; % kill if it overlaps stuff
246 elseif strcmpi(arg, 'v6')
247 % passed in subplot(m,n,p,'v6')
248 % since obeying position will remove the axes from the grid just set
249 % useAutoLayout to false to skip adding it to the grid to start with
250 warning(['MATLAB:', mfilename, ':DeprecatedV6Argument'],...
251 ['The ''v6'' argument to %s is deprecated,',...
252 ' and will no longer be supported in a future release.'], upper(mfilename));
253 useAutoLayout = false;
254 killSiblings = 1; % kill if it overlaps stuff
255 else
256 % passed in prop-value pairs
257 killSiblings = 1;
258 pvpairs = varargin;
259 par = find(strncmpi('Parent', pvpairs(1 : 2 : end), 6));
260 if any(par)
261 % If the parent is passed in explicitly, don't create a new figure
262 % when the "NextPlot" property is set to "new" in the figure.
263 explicitParent = true;
264 parent = varargin{2 * par(1)};
265 ancestorFigure = ancestor(parent, 'figure');
266 end
267 end
268 else
269 % Passed in "Position" syntax with P/V pairs
270 % The arguments MUST be the string 'position' and a 4-element vector:
271 if (strcmpi(nRows, 'position'))
272 pos_size = size(nCols);
273 if (pos_size(1) * pos_size(2) == 4)
274 position = nCols;
275 explicitPosition = true;
276 else
277 error(id('InvalidPositionParameter'), ...
278 'Position must be of the form [left bottom width height].')
279 end
280 else
281 error(id('UnknownOption'), 'Unknown command option.')
282 end
283 killSiblings = 1; % Kill overlaps here also.
284 useAutoLayout = false;
285 pvpairs = [{plotId}, varargin];
286 par = find(strncmpi('Parent', pvpairs(1 : 2 : end), 6));
287 if any(par)
288 % If the parent is passed in explicitly, don't create a new figure
289 % when the "NextPlot" property is set to "new" in the figure.
290 explicitParent = true;
291 parent = pvpairs{2 * par(1)};
292 ancestorFigure = ancestor(parent, 'figure');
293 end
294 end
295 end
296
297 % if we recovered an identifier earlier, use it:
0.01 249 298 if ~isempty(h) && ~moveAxis
299 parent = get(h, 'Parent');
300 ancestorFigure = ancestor(h, 'figure');
301 set(ancestorFigure, 'CurrentAxes', h);
249 302 else % if we haven't recovered position yet, generate it from mnp info:
249 303 if isempty(parent)
304 parent = gcf;
305 ancestorFigure = parent;
306 end
249 307 if isempty(position)
249 308 if min(plotId) < 1
309 error(id('SubplotIndexTooSmall'), 'Illegal plot number.')
249 310 elseif max(plotId) > nCols * nRows
311 error(id('SubplotIndexTooLarge'), 'Index exceeds number of subplots.');
249 312 else
313
249 314 row = (nRows - 1) - fix((plotId - 1) / nCols);
249 315 col = rem(plotId - 1, nCols);
316
317 % get default axes position in normalized units
318 % If we have checked this quanitity once, cache it.
249 319 if ~isappdata(ancestorFigure, 'SubplotDefaultAxesLocation')
16 320 if ~strcmp(get(ancestorFigure, 'DefaultAxesUnits'), 'normalized')
321 tmp = axes;
322 set(tmp, 'Units', 'normalized')
323 def_pos = get(tmp, 'Position');
324 delete(tmp)
16 325 else
16 326 def_pos = get(ancestorFigure, 'DefaultAxesPosition');
16 327 end
16 328 setappdata(ancestorFigure, 'SubplotDefaultAxesLocation', def_pos);
233 329 else
233 330 def_pos = getappdata(ancestorFigure, 'SubplotDefaultAxesLocation');
233 331 end
332
333 % compute outerposition and insets relative to figure bounds
249 334 rw = max(row) - min(row) + 1;
249 335 cw = max(col) - min(col) + 1;
249 336 width = def_pos(3) / (nCols - inset(1) - inset(3));
249 337 height = def_pos(4) / (nRows - inset(2) - inset(4));
249 338 inset = inset .* [width, height, width, height];
249 339 outerpos = [def_pos(1) + min(col) * width - inset(1), ...
340 def_pos(2) + min(row) * height - inset(2), ...
341 width * cw, height * rw];
342
343 % compute inner position
249 344 position = [outerpos(1 : 2) + inset(1 : 2), ...
345 outerpos(3 : 4) - inset(1 : 2) - inset(3 : 4)];
346
249 347 end
249 348 end
249 349 end
350
351 % kill overlapping siblings if mnp specifier was used:
0.02 249 352 nextstate = get(ancestorFigure, 'NextPlot');
353
249 354 if strncmp(nextstate, 'replace', 7)
355 nextstate = 'add';
249 356 elseif strncmp(nextstate, 'new', 3)
357 killSiblings = 0;
358 end
359
249 360 if killSiblings
249 361 if delayDestroy
362 if nargout
363 error(id('TooManyOutputs'), ...
364 'Function called with too many output arguments.')
365 else
366 set(ancestorFigure, 'NextPlot', 'replace');
367 return
368 end
369 end
0.09 249 370 sibs = datachildren(parent);
249 371 newcurrent = [];
249 372 for i = 1 : length(sibs)
373 % Be aware that handles in this list might be destroyed before
374 % we get to them, because of other objects' DeleteFcn callbacks...
0.04 1400 375 if ishghandle(sibs(i), 'axes')
1400 376 units = get(sibs(i), 'Units');
0.52 1400 377 sibpos = get(sibs(i), 'Position');
378 % If a legend or colorbar has resized the axes, use the original axes
379 % position as the "Position" property:
0.01 1400 380 if ~explicitPosition
0.02 1400 381 if isappdata(sibs(i), 'LegendColorbarExpectedPosition') && ...
382 isequal(getappdata(sibs(i), 'LegendColorbarExpectedPosition'), get(sibs(i), 'Position'))
36 383 sibinset = getappdata(sibs(i), 'LegendColorbarOriginalInset');
36 384 if isempty(sibinset)
385 % during load the appdata might not be present
36 386 sibinset = get(get(sibs(i), 'Parent'), 'DefaultAxesLooseInset');
36 387 end
0.01 36 388 sibinset = offsetsInUnits(sibs(i), sibinset, 'normalized', get(sibs(i), 'Units'));
0.02 36 389 if strcmpi(get(sibs(i), 'ActivePositionProperty'), 'position')
36 390 pos = get(sibs(i), 'Position');
36 391 loose = get(sibs(i), 'LooseInset');
0.01 36 392 opos = getOuterFromPosAndLoose(pos, loose, get(sibs(i), 'Units'));
36 393 if strcmp(get(sibs(i), 'Units'), 'normalized')
36 394 sibinset = [opos(3 : 4), opos(3 : 4)] .* sibinset;
36 395 end
36 396 sibpos = [opos(1 : 2) + sibinset(1 : 2), opos(3 : 4) - sibinset(1 : 2) - sibinset(3 : 4)];
36 397 end
36 398 end
0.01 1400 399 end
0.01 1400 400 if ~strcmp(units, 'normalized')
401 sibpos = hgconvertunits(ancestorFigure, sibpos, units, 'normalized', parent);
402 end
1400 403 intersect = 1;
1400 404 if ((position(1) >= sibpos(1) + sibpos(3) - tol) || ...
405 (sibpos(1) >= position(1) + position(3) - tol) || ...
406 (position(2) >= sibpos(2) + sibpos(4) - tol) || ...
407 (sibpos(2) >= position(2) + position(4) - tol))
0.01 1244 408 intersect = 0;
1244 409 end
0.01 1400 410 if intersect
411 % position is the proposed position of an axes, and
412 % sibpos is the current position of an existing axes.
413 % Since the bounding boxes of position and sibpos overlap,
414 % we must determine whether to delete the sibling sibs(i)
415 % whose normalized position is sibpos.
416
417 % First of all, we check whether we must kill the sibling
418 % "no matter what."
156 419 if (killSiblings == 2)
420 delete(sibs(i));
421
422 % If the proposed and existing axes overlap exactly, we do
423 % not kill the sibling. Rather we shall ensure later that
424 % this sibling axes is set as the 'CurrentAxes' of its
425 % ancestorFigure.
426
427 % Next we check for a partial overlap.
156 428 elseif (any(abs(sibpos - position) > tol))
429 % The proposed and existing axes partially overlap.
430 % Since the proposed and existing axes could each be
431 % "grid-generated" or "explicitly-specified", we must
432 % consider four possibilities for the overlap of
433 % "proposed" vs. "existing", i.e.
434 % (1) "grid-generated" vs. "grid-generated"
435 % (2) "grid-generated" vs. "explicitly-specified"
436 % (3) "explicitly-specified" vs. "grid-generated"
437 % (4) "explicitly-specified" vs. "explicitly-specified"
438
439 % If the position of the proposed axes is
440 % "explicitly-specified", then the only condition that
441 % avoids killing the sibling is an exact overlap.
442 % However, we know that the overlap is partial.
72 443 if (explicitPosition)
444 delete(sibs(i));
72 445 else
446 % We know that the position of the proposed axes is
447 % "grid-generated".
448
0.01 72 449 grid = getappdata(parent, 'SubplotGrid');
450 % The SubplotGrid maintains an array of axes
451 % handles, one per grid location. Axes that span
452 % multiple grid locations do not store handles in
453 % the SubplotGrid.
454
72 455 if isempty(grid) || ~any(grid(:) == sibs(i)) || ...
456 size(grid, 1) ~= nRows || size(grid, 2) ~= nCols || ...
457 ~isscalar(row) || ~isscalar(col)
458 % If the sibling cannot be found in the grid, we
459 % kill the sibling. Otherwise, the proposed and
460 % existing axes are "grid-generated". If we
461 % are changing the size of the grid, we kill
462 % the sibling. Otherwise, "plotId" may be a
463 % vector of multiple grid locations, which
464 % causes a partial overlap between the proposed
465 % and existing axes, so we kill the sibling.
466
467 % This check recognizes that there may be
468 % labels, colorbars, legends, etc. attached to
469 % the existing axes that have affected its
470 % position. In such a case, we do not kill the
471 % sibling.
0.02 66 472 delete(sibs(i));
66 473 end
72 474 end
72 475 end
156 476 if ishghandle(sibs(i))
477 % if this axes overlaps the other one exactly then
90 478 if ~isempty(newcurrent) && ishghandle(newcurrent)
479 delete(newcurrent);
480 end
90 481 newcurrent = sibs(i);
90 482 end
156 483 end
1400 484 end
1400 485 end
249 486 if ~isempty(newcurrent) && ishghandle(newcurrent)
0.27 90 487 set(ancestorFigure, 'CurrentAxes', newcurrent);
90 488 createAxis = false;
90 489 end
249 490 set(ancestorFigure, 'NextPlot', nextstate);
0.01 249 491 end
492
493 % create the axes:
249 494 if createAxis
0.01 159 495 if strcmp(nextstate, 'new') && ~explicitParent
496 parent = figure;
497 ancestorFigure = parent;
498 end
0.20 159 499 ax = axes('Units', 'normalized', 'Position', position, ...
500 'LooseInset', inset, 'Parent', parent);
501 % TODO: Get axes to accept position args on command line
0.01 159 502 set(ax, 'Units', get(double(ancestorFigure), 'DefaultAxesUnits'))
159 503 if useAutoLayout
0.28 159 504 addAxesToGrid(ax, nRows, nCols, row, col, position);
159 505 end
159 506 if ~isempty(pvpairs)
507 set(ax, pvpairs{:});
508 end
90 509 elseif moveAxis && ~preventMove
510 ax = h;
511 units = get(h, 'Units');
512 set(h, 'Units', 'normalized', 'Position', position, ...
513 'LooseInset', inset, 'Parent', parent);
514 set(h, 'Units', units);
515 if useAutoLayout
516 addAxesToGrid(ax, nRows, nCols, row, col, position);
517 end
518 if ~isempty(pvpairs)
519 set(h, pvpairs{:});
520 end
90 521 else
522 % this should only happen with subplot(H)
0.01 90 523 ax = get(ancestorFigure, 'CurrentAxes');
90 524 end
525
526 % return identifier, if requested:
249 527 if(nargout > 0)
3 528 theAxis = ax;
3 529 end
249 530 end
249 531 end
Other subfunctions in this file are not included in this listing.