-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget_axes_width.m
38 lines (31 loc) · 962 Bytes
/
get_axes_width.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function pixels = get_axes_width(h)
% pixels = get_axes_width(h)
%
% Returns the width of the axes object, h, in pixels.
%
% --- Change Log ---
%
% 2014-06-04: Avoids changing axes units where possible.
% Changes copyright 2014 Tucker McClure.
%
% 2013-03-15: Original. Copyright 2013, The MathWorks, Inc.
%
% ---
%
% Copyright 2014, The MathWorks, Inc. and Tucker McClure
% Record the current axes units setting.
axes_units = get(h, 'Units');
% We should change the units only if they're not already set to pixels.
change_units = ~strcmp(axes_units, 'pixels');
% Change axes units to pixels.
if change_units
set(h, 'Units', 'pixels'); % This triggers other callbacks.
end
% Get axes width in pixels.
axes_position = get(h, 'Position');
pixels = round(axes_position(3));
% Return the units.
if change_units
set(h, 'Units', axes_units); % This triggers other callbacks.
end
end