-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex_guide_timergui.m
206 lines (170 loc) · 7.59 KB
/
ex_guide_timergui.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
function varargout = ex_guide_timergui(varargin)
% EX_GUIDE_TIMERGUI - Execute graphic updates at regular intervals
% MATLAB code for ex_guide_timergui.fig
% EX_GUIDE_TIMERGUI, by itself, creates a new EX_GUIDE_TIMERGUI
% or raises the existing singleton*.
%
% H = EX_GUIDE_TIMERGUI returns the handle to a new EX_GUIDE_TIMERGUI
% or the handle to the existing singleton*.
%
% EX_GUIDE_TIMERGUI('CALLBACK',hObject,eventData,handles,...) calls
% the local function named CALLBACK in EX_GUIDE_TIMERGUI.M with
% the given input arguments.
%
% EX_GUIDE_TIMERGUI('Property','Value',...) creates a new
% EX_GUIDE_TIMERGUI or raises the existing singleton*.
% Starting from the left, property value pairs are applied to the
% GUI before ex_guide_timergui_OpeningFcn gets called.
% An unrecognized property name or invalid value makes property
% application stop. All inputs are passed to
% ex_guide_timergui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows
% only one instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES, TIMER
% Last Modified by GUIDE v2.5 23-Sep-2016 13:36:12
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ex_guide_timergui_OpeningFcn, ...
'gui_OutputFcn', @ex_guide_timergui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before ex_guide_timergui is made visible.
function ex_guide_timergui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ex_guide_timergui (see VARARGIN)
% Choose default command line output for ex_guide_timergui
handles.output = hObject;
% START USER CODE
% Create a timer object to fire at 1/10 sec intervals
% Specify function handles for its start and run callbacks
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Initial period is 1 sec.
'TimerFcn', {@update_display,hObject}); % Specify callback function
% Initialize slider and its readout text field
set(handles.periodsldr,'Min',0.01,'Max',2)
set(handles.periodsldr,'Value',get(handles.timer,'Period'))
set(handles.slidervalue,'String',...
num2str(get(handles.periodsldr,'Value')))
% Create a surface plot of peaks data. Store handle to it.
handles.surf = surf(handles.display,peaks);
% END USER CODE
% Update handles structure
guidata(hObject,handles);
% --- Outputs from this function are returned to the command line.
function varargout = ex_guide_timergui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in startbtn.
function startbtn_Callback(hObject, eventdata, handles)
% hObject handle to startbtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% START USER CODE
% Only start timer if it is not running
% if strcmp(get(handles.timer, 'Running'), 'off')
% start(handles.timer);
% end
start(handles.timer);
% END USER CODE
% --- Executes on button press in stopbtn.
function stopbtn_Callback(hObject, eventdata, handles)
% hObject handle to stopbtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% START USER CODE
% Only stop timer if it is running
if strcmp(get(handles.timer, 'Running'), 'on')
stop(handles.timer);
end
% END USER CODE
% --- Executes on slider movement.
function periodsldr_Callback(hObject, eventdata, handles)
% hObject handle to periodsldr (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% START USER CODE
% Read the slider value
period = get(handles.periodsldr,'Value');
% Timers need the precision of periods to be greater than about
% 1 millisecond, so truncate the value returned by the slider
period = period - mod(period,.01);
% Set slider readout to show its value
set(handles.slidervalue,'String',num2str(period))
% If timer is on, stop it, reset the period, and start it again.
if strcmp(get(handles.timer, 'Running'), 'on')
stop(handles.timer);
set(handles.timer,'Period',period)
start(handles.timer)
else % If timer is stopped, reset its period only.
set(handles.timer,'Period',period)
end
% END USER CODE
% --- Executes during object creation, after setting all properties.
function periodsldr_CreateFcn(hObject, eventdata,handles)
% hObject handle to periodsldr (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(groot,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% START USER CODE
function update_display(hObject,eventdata,hfigure)
% Timer timer1 callback, called each time timer iterates.
% Gets surface Z data, adds noise, and writes it back to surface object.
handles = guidata(hfigure);
Z = get(handles.surf,'ZData');
Z = Z + 0.1*randn(size(Z));
set(handles.surf,'ZData',Z);
% END USER CODE
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% START USER CODE
% Necessary to provide this function to prevent timer callback
% from causing an error after GUI code stops executing.
% Before exiting, if the timer is running, stop it.
if strcmp(get(handles.timer, 'Running'), 'on')
stop(handles.timer);
end
% Destroy timer
delete(handles.timer)
% END USER CODE
% Hint: delete(hObject) closes the figure
delete(hObject);
% --- Executes during object creation, after setting all properties.
function display_CreateFcn(hObject, eventdata, handles)
% hObject handle to display (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate display
% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)