-
Notifications
You must be signed in to change notification settings - Fork 9
/
scalebar.m
236 lines (210 loc) · 8.88 KB
/
scalebar.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
function h = scalebar(varargin)
%SCALEBAR creates a scalebar on an axes
%
% SCALEBAR
% SCALEBAR OFF
% SCALEBAR(PARAMETER, VALUE, ...)
% SCALEBAR(HAXES, PARAMETER, VALUE, ...)
% H = SCALEBAR(...)
%
% Draws a scalebar on the axes and returns handle to the scalebar. The
% DataAspectRatio property of the axes must be set to [1 1 1] and the view
% must be in 2D. All parameters are optional (note the default values
% below). SCALEBAR OFF deletes the current scalebar.
%
% PARAMETER/VALUE pairs
% hAxes: handle to the axes (defaults to current axes)
% ScaleLength: length to show (in data units) (defaults to ~10% of the
% x-axis limit range)
% ScaleLengthRatio: ScaleLength/range(XLim)
% Location: location of the scalebar. Possible values are
% northeast (default)
% northwest
% southeast
% southwest
% [x y] data coordinates
% Colour: colour of scalebar in 1x3 RGB array (default is [0 0 0])
% Bold: draw with bold text and linewidth=2.
% True or false(default)
%
% Note: SCALEBAR sets the XLimMode and YLimMode of the axes to manual.
%
% Created 10 November 2009 by Amanda Ng
% DELETE SCALEBAR IF ONE EXISTS
delete(findobj(gca,'tag','scalebar'));
% RETURN IF 'OFF' WAS REQUESTED
if nargin>0 && strcmpi(varargin{1},'off')
return;
end
% CONSTANTS
directions = {'northwest','northeast','southeast','southwest'};
% SET PARAMETERS TO DEFAULTS
hAxes = gca;
scalelength = 0;
scalelengthratio = 0.1;
location = 'northeast';
colour = [0 0 0];
linewidth = 0.5;
fontweight = 'normal';
% PROCESS ARGUMENTS
if nargin>0
args = {};
% Process if arguments given as "hAxes, ..."
if ishandle(varargin{1}) %hAxes
args{length(args)+1}='hAxes';
args{length(args)+1}=varargin{1};
varargin = varargin(2:end);
end
args=[args varargin];
% Process arguments
for n=1:2:length(args)
parameter = args{n};
value = args{n+1};
switch lower(parameter)
case 'haxes'
if ~ishandle(value)
error 'HAXES is not a valid Axes handle'
elseif ~strcmpi(get(value,'type'),'axes')
error 'HAXES is not a valid Axes handle'
elseif strcmpi(get(value,'tag'),'colorbar')
error 'HAXES is a handle to a colorbar'
else
hAxes = value;
end
case 'scalelength'
if ~isnumeric(value)
error 'SCALELENGTH must be a numeric value'
end
scalelength = value;
case 'scalelengthratio'
if ~isnumeric(value)
error 'SCALELENGTHRATIO must be a numeric value'
end
scalelengthratio = value;
case 'location'
if ~(numel(value)==2 && isnumeric(value)) && ...
isempty(strmatch(lower(value),directions,'exact'))
error 'unrecognised value for LOCATION'
end
location = value;
case 'colour'
if numel(value)~=3 || ~isnumeric(value)
error 'COLOUR must be a 1x3 representation of an RGB colour'
end
colour = value;
case 'bold'
if ischar(value) && strcmpi(value,'true') || ...
(islogical(value) || isnumeric(value)) && value
linewidth=2;
fontweight='bold';
end
otherwise
error(['unrecognised parameter: ' parameter]);
end
end
end
% CHECK IF DATAASPECTRATIO IS [1 1 1]
if ~all(get(hAxes,'DataAspectRatio')==1)
error 'The Axes property DataAspectRatio must be set to [1 1 1]'
end
% CHECK IF VIEW IS IN 2D
[az el] = view(hAxes);
if el~=90
error 'The Axes must be in 2D view'
end
%GET IMAGE AND AXES DATA
axeslims = [get(hAxes,'xlim')' get(hAxes,'ylim')'];
axesdir = [1 1];
if strcmpi(get(hAxes,'XDir'),'reverse')
axeslims(:,1) = flipud(axeslims(:,1));
axesdir(1) = -1;
end
if strcmpi(get(hAxes,'YDir'),'reverse')
axeslims(:,2) = flipud(axeslims(:,2));
axesdir(2) = -1;
end
% CALCULATE SCALELENGTH
if scalelength==0
sl = range(axeslims(:,1))*scalelengthratio;
slorder = 10^floor(log10(sl));
scalelength = round(sl/slorder)*slorder;
else
scalelengthratio = scalelength/range(axeslims(:,1));
end
%SET UP POSITIONING
if ischar(location)
switch location
case 'northeast'
anchor = [axeslims(2,1) - axesdir(1)*range(axeslims(:,1))*0.05, ...
axeslims(2,2) - axesdir(2)*range(axeslims(:,2))*0.05];
direction = 'southwest';
case 'northwest'
anchor = [axeslims(1,1) + axesdir(1)*range(axeslims(:,1))*0.05, ...
axeslims(2,2) - axesdir(2)*range(axeslims(:,2))*0.05];
direction = 'southeast';
case 'southwest'
anchor = [axeslims(1,1) + axesdir(1)*range(axeslims(:,1))*0.05, ...
axeslims(1,2) + axesdir(2)*range(axeslims(:,2))*0.05];
direction = 'northeast';
case 'southeast'
anchor = [axeslims(2,1) - axesdir(1)*range(axeslims(:,1))*0.05, ...
axeslims(1,2) + axesdir(2)*range(axeslims(:,2))*0.05];
direction = 'northwest';
end
else
anchor = location;
if location
dirToCentre = min(axeslims)+range(axeslims)/2 - location.*axesdir;
direction = directions{ceil((-1*atan2(dirToCentre(2),dirToCentre(1))+pi)/(2*pi)*4)};
end
end
linepos = [anchor; anchor];
if ~isempty(strfind(direction,'east'))
linepos(2,1) = linepos(2,1)+axesdir(1)*scalelength;
else
linepos(1,1) = linepos(1,1)-axesdir(1)*scalelength;
end
ends(:,:,1) = [linepos(1,:); linepos(1,:)];
ends(:,:,2) = [linepos(2,:); linepos(2,:)];
if ~isempty(strfind(direction,'north'))
ends(2,2,:) = ends(2,2,:)-axesdir(2)*0.1*scalelength;
textalignment = {'bottom', 'center'};
else
ends(2,2,:) = ends(2,2,:)+axesdir(2)*0.1*scalelength;
textalignment = {'top', 'center'};
end
% DRAW SCALEBAR
set(gca,'xlimmode','manual','ylimmode','manual');
hg = hggroup('tag','scalebar');
line(linepos(:,1), linepos(:,2), 'color', colour, 'linewidth', linewidth, 'parent', hg);
line(ends(:,1,1), ends(:,2,1), 'color', colour, 'linewidth', linewidth, 'parent', hg);
line(ends(:,1,2), ends(:,2,2), 'color', colour, 'linewidth', linewidth, 'parent', hg);
text(linepos(1,1),linepos(1,2),0,'0','verticalalignment',textalignment{1},'horizontalalignment',textalignment{2}, 'color', colour, 'fontweight', fontweight, 'parent', hg);
text(linepos(2,1),linepos(2,2),0,num2str(scalelength),'verticalalignment',textalignment{1},'horizontalalignment',textalignment{2}, 'color', colour, 'fontweight', fontweight, 'parent', hg);
if nargout>0
h = hg;
end
% SETUP DELETE CALLBACK
set(hg,'DeleteFcn',@deleteScaleBar)
% SETUP LISTENER TO RESET SCALEBAR ON CHANGE OF AXES LIMITS
hL(1) = addlistener(hAxes,'YLim','PostSet',@(src,event) resetScaleBar(src,event,hg));
% SET USERDATA
udata = {'ScaleLengthRatio',scalelengthratio;...
'AnchorRatio',[(anchor(1)-min(axeslims(:,1)))/range(axeslims(:,1)) (anchor(2)-min(axeslims(:,2)))/range(axeslims(:,2))];...
'Colour',colour;...
'Listeners',hL};
set(hg,'UserData',udata);
% CALLBACK FUNCTIONS
function deleteScaleBar(src,event)
udata = get(src,'UserData');
delete(udata{strcmpi(udata(:,1),'Listeners'),2});
function resetScaleBar(src,event,SB)
udata = get(SB,'UserData');
hAxes = get(SB,'parent');
delete(SB);
axeslims = [get(hAxes,'xlim')' get(hAxes,'ylim')'];
scalelengthratio = udata{strcmpi(udata(:,1),'ScaleLengthRatio'),2};
anchorratio = udata{strcmpi(udata(:,1),'AnchorRatio'),2};
location = [anchorratio(1)*range(axeslims(:,1))+axeslims(1,1) anchorratio(2)*range(axeslims(:,2))+axeslims(1,2)];
colour = udata{strcmpi(udata(:,1),'Colour'),2};
scalebar(hAxes,'ScaleLengthRatio',scalelengthratio,'Location',location,'Colour',colour);