-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththermometer.m
166 lines (143 loc) · 5.04 KB
/
thermometer.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
function varargout = thermometer(hAx,Trange,Tinit);
% THERMOMETER Create a graphical thermometer display
%
% THERMOMETER makes it easy to include a thermometer display in your
% real-time data acquisition and analysis application. It takes
% 2 steps to use THERMOMETER. First, you initialize the thermometer with basic
% information needed for the display (display range and initial temperature).
% After that, all you need to do is pass the current temperature to the thermometer.
%
% This documentation starts with the simplest syntax for the two steps,
% then provides a few more advanced options.
%
% STEP 1: Initialize the thermometer
% hAx = THERMOMETER(Trange) converts the current axes to a graphical
% thermometer display. If no figures exist, a new one will be created.
% Trange = [Tmin Tmax] specifies the minimum and
% maximum of the temperature scale. Alternatively, Trange = [Tmin Tincr
% Tmax] specifies the scale increment, too.
% hAx is a required output in this case, as it will be passed to all subsequent
% calls to THERMOMETER. Specifically, hAx is the handle to the axis containing
% the thermometer.
%
% STEP 2: Update the thermometer
% THERMOMETER(hAx, T) updates the thermometer display in axis hAx to the
% specified temperature T. hAx was either passed to THERMOMETER during
% initialization, or was returned from the initialization call.
%
% THERMOMETER(hAx, Trange) creates a new graphical thermometer display in
% the specified axis, hAx. Trange is as defined above.
%
% For users who would like more control over the display,
% [hAx,hPatch] = THERMOMETER(...)
% returns a handle to the patch object used to represent the thermometer
% value (i.e., the mercury).
%
%
% Examples:
% hAx = thermometer([0 30]); %Create a thermometer in a new figure. Range is 0 to 30 degrees
% thermometer(hAx,15); %Set current temperature to 15 degrees
%
% % Create figure and axes
% figure;
% hAx = axes('Position',[.1 .1 .1 .8]);
%
% %Create a thermometer on axes hAx. Scale goes from 0 to 100 in steps
% % of 20. Initial value is 40.
% [hAx,hPatch] = thermometer(hAx,[0 20 100],40);
%
% % Set a new color scheme
% set(hAx,'Color','y'); % Yellow background
% set(hPatch,'FaceColor','g') % Green mercury
% Michelle Hirsch
% Copyright 2003 - 2014 The MathWorks, Inc
narginchk(1,3)
msg = nargchk(1,3,nargin);
error(msg)
%If first input argument is Trange
if length(hAx)>1
if nargout==0
error(['I really want to give you the axis handle. ' ...
'Without it, you won''t be able to update the thermometer. ' ...
'So, let''s try this again, and call me with one output!'])
end;
if nargin==1 %Did not specify initial temperature
Tinit = hAx(1) + .1*(hAx(end)-hAx(1)); % Default Value
else
Tinit = Trange; %Re-define input argument
end;
Trange = hAx;
%% Create a figure if none exist
% Otherwise, use current axes.
hFigs = findobj(0,'Type','figure');
if isempty(hFigs)
hAx = localCreateFig;
else
hAx = gca;
end;
[hAx,hPatch] = thermometer(hAx,Trange,Tinit);
varargout{1} = hAx;
else
if ~strcmp(get(hAx,'Type'),'axes')
error('With 2 or 3 input arguments, the first argument must be an axis handle');
end;
if length(Trange)==2 | length(Trange)==3
if length(Trange)==3
incr = Trange(2);
Trange = Trange([1 3]);
else
incr=5;
end;
if nargin==2 %Did not specify initial temperature
Tinit = Trange(1) + .1*diff(Trange); % Default Value
end;
hPatch = localInitAxes(hAx,Trange,incr,Tinit);
elseif length(Trange)==1
Tnew = Trange;
hPatch = localUpdate(hAx,Tnew); %Trange is now the new temperature
else
error('Second input argument must be [Tmin Tmax] or Tnew');
end;
if nargout
varargout{1} = hAx;
varargout{2} = hPatch;
end;
end
function hPatch = localInitAxes(hAx,Trange,incr,Tinit)
%Are there any thermometer patches on this axis already? If so, delete
%them
therms = findobj(hAx,'Type','patch','Tag','Thermometer');
if ~isempty(therms)
delete(therms)
end;
set(hAx, ...
'Color',[.8 .8 .8], ...
'TickLength', [0.0500 0.1000], ...
'XLim',[0 1], ...
'XColor',[.8 .8 .8], ...
'XLimMode','manual', ...
'XTick',0, ...
'YLim',Trange, ...
'YLimMode','manual', ...
'YMinorTick','on', ...
'YTick',Trange(1):incr:Trange(2));
hPatch = patch([0 1 1 0],[0 0 Tinit Tinit],'r','Tag','Thermometer');
alpha(hPatch,.6)
setappdata(hAx,'PatchHandle',hPatch);
function hPatch = localUpdate(hAx,T)
hPatch = getappdata(hAx,'PatchHandle');
%Force within valid temperature range
yl = get(hAx,'YLim');
if T<yl(1)
T=yl(1);
elseif T>yl(2)
T=yl(2);
end;
set(hPatch,'YData',[0 0 T T]);
function hAx = localCreateFig
figure('Position',[200 200 75 200], ...
'Toolbar','none', ...
'MenuBar','none', ...
'NumberTitle','off');
hAx = axes('Position',[.3 .1 .4 .8]);