source: issm/oecreview/Archive/14064-14311/ISSM-14242-14243.diff

Last change on this file was 14312, checked in by Mathieu Morlighem, 13 years ago

Added 14064-14311

File size: 2.9 KB
RevLine 
[14312]1Index: ../trunk-jpl/src/m/plot/plotboxpos.m
2===================================================================
3--- ../trunk-jpl/src/m/plot/plotboxpos.m (revision 0)
4+++ ../trunk-jpl/src/m/plot/plotboxpos.m (revision 14243)
5@@ -0,0 +1,91 @@
6+function pos = plotboxpos(h)
7+%PLOTBOXPOS Returns the position of the plotted axis region
8+%
9+% pos = plotboxpos(h)
10+%
11+% This function returns the position of the plotted region of an axis,
12+% which may differ from the actual axis position, depending on the axis
13+% limits, data aspect ratio, and plot box aspect ratio. The position is
14+% returned in the same units as the those used to define the axis itself.
15+% This function can only be used for a 2D plot.
16+%
17+% Input variables:
18+%
19+% h: axis handle of a 2D axis (if ommitted, current axis is used).
20+%
21+% Output variables:
22+%
23+% pos: four-element position vector, in same units as h
24+
25+% Copyright 2010 Kelly Kearney
26+
27+% Check input
28+
29+if nargin < 1
30+ h = gca;
31+end
32+
33+if ~ishandle(h) || ~strcmp(get(h,'type'), 'axes')
34+ error('Input must be an axis handle');
35+end
36+
37+% Get position of axis in pixels
38+
39+currunit = get(h, 'units');
40+set(h, 'units', 'pixels');
41+axisPos = get(h, 'Position');
42+set(h, 'Units', currunit);
43+
44+% Calculate box position based axis limits and aspect ratios
45+
46+darismanual = strcmpi(get(h, 'DataAspectRatioMode'), 'manual');
47+pbarismanual = strcmpi(get(h, 'PlotBoxAspectRatioMode'), 'manual');
48+
49+if ~darismanual && ~pbarismanual
50+
51+ pos = axisPos;
52+
53+else
54+
55+ dx = diff(get(h, 'XLim'));
56+ dy = diff(get(h, 'YLim'));
57+ dar = get(h, 'DataAspectRatio');
58+ pbar = get(h, 'PlotBoxAspectRatio');
59+
60+ limDarRatio = (dx/dar(1))/(dy/dar(2));
61+ pbarRatio = pbar(1)/pbar(2);
62+ axisRatio = axisPos(3)/axisPos(4);
63+
64+ if darismanual
65+ if limDarRatio > axisRatio
66+ pos(1) = axisPos(1);
67+ pos(3) = axisPos(3);
68+ pos(4) = axisPos(3)/limDarRatio;
69+ pos(2) = (axisPos(4) - pos(4))/2 + axisPos(2);
70+ else
71+ pos(2) = axisPos(2);
72+ pos(4) = axisPos(4);
73+ pos(3) = axisPos(4) * limDarRatio;
74+ pos(1) = (axisPos(3) - pos(3))/2 + axisPos(1);
75+ end
76+ elseif pbarismanual
77+ if pbarRatio > axisRatio
78+ pos(1) = axisPos(1);
79+ pos(3) = axisPos(3);
80+ pos(4) = axisPos(3)/pbarRatio;
81+ pos(2) = (axisPos(4) - pos(4))/2 + axisPos(2);
82+ else
83+ pos(2) = axisPos(2);
84+ pos(4) = axisPos(4);
85+ pos(3) = axisPos(4) * pbarRatio;
86+ pos(1) = (axisPos(3) - pos(3))/2 + axisPos(1);
87+ end
88+ end
89+end
90+
91+% Convert plot box position to the units used by the axis
92+
93+temp = axes('Units', 'Pixels', 'Position', pos, 'Visible', 'off', 'parent', get(h, 'parent'));
94+set(temp, 'Units', currunit);
95+pos = get(temp, 'position');
96+delete(temp);
97\ No newline at end of file
Note: See TracBrowser for help on using the repository browser.