comparison m-toolbox/sltpda/loopgui.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function varargout = loopgui(varargin)
2 % LOOPGUI M-file for loopgui.fig
3 % LOOPGUI, by itself, creates a new LOOPGUI or raises the existing
4 % singleton*.
5 %
6 % H = LOOPGUI returns the handle to a new LOOPGUI or the handle to
7 % the existing singleton*.
8 %
9 % LOOPGUI('CALLBACK',hObject,eventData,handles,...) calls the local
10 % function named CALLBACK in LOOPGUI.M with the given input arguments.
11 %
12 % LOOPGUI('Property','Value',...) creates a new LOOPGUI or raises the
13 % existing singleton*. Starting from the left, property value pairs are
14 % applied to the GUI before loopgui_OpeningFunction gets called. An
15 % unrecognized property name or invalid value makes property application
16 % stop. All inputs are passed to loopgui_OpeningFcn via varargin.
17 %
18 % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
19 % instance to run (singleton)".
20 %
21 % See also: GUIDE, GUIDATA, GUIHANDLES
22
23 % Edit the above text to modify the response to help loopgui
24
25 % Last Modified by GUIDE v2.5 12-Jun-2007 20:24:04
26
27 % Begin initialization code - DO NOT EDIT
28 gui_Singleton = 0;
29 gui_State = struct('gui_Name', mfilename, ...
30 'gui_Singleton', gui_Singleton, ...
31 'gui_OpeningFcn', @loopgui_OpeningFcn, ...
32 'gui_OutputFcn', @loopgui_OutputFcn, ...
33 'gui_LayoutFcn', [] , ...
34 'gui_Callback', []);
35 if nargin && ischar(varargin{1})
36 gui_State.gui_Callback = str2func(varargin{1});
37 end
38
39 if nargout
40 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
41 else
42 gui_mainfcn(gui_State, varargin{:});
43 end
44 % End initialization code - DO NOT EDIT
45
46
47 % --- Executes just before loopgui is made visible.
48 function loopgui_OpeningFcn(hObject, eventdata, handles, varargin)
49 % This function has no output args, see OutputFcn.
50 % hObject handle to figure
51 % eventdata reserved - to be defined in a future version of MATLAB
52 % handles structure with handles and user data (see GUIDATA)
53 % varargin command line arguments to loopgui (see VARARGIN)
54
55 % Choose default command line output for loopgui
56 handles.output = hObject;
57
58 % Update handles structure
59 guidata(hObject, handles);
60
61 % UIWAIT makes loopgui wait for user response (see UIRESUME)
62 % uiwait(handles.main);
63
64 set(handles.loopLevelSelect, 'String', {'1','2','3','4','5'});
65 set(handles.loopLevelSelect, 'Value', 1);
66
67 % get my block handles
68 myh = get_param(gcb, 'handle');
69 setappdata(handles.main, 'loopBlockHandle', myh);
70
71
72 % parse model elements and get block list that
73 % have parameters that can be looped over
74 blockhandles = find_system(gcs, 'FindAll', 'on', 'type', 'block');
75 blocks = [];
76 k = 1;
77 for j=1:length(blockhandles)
78 b = blockhandles(j);
79 maskType = get(b, 'MaskType');
80 if ~strcmp(maskType, 'loop')
81 maskParams = get(b, 'MaskNames');
82 get(b)
83 if ~isempty(maskParams)
84 name = get(b, 'Name');
85 if ~strcmp(name, 'variable')
86 parent = get(b, 'Parent');
87 name = [parent '/' name];
88 blocks(k).handle = b;
89 blocks(k).name = name;
90 k = k+1;
91 end
92 end
93 end
94 end
95
96 setappdata(handles.main, 'blocks', blocks);
97
98 % Fill list
99 bnames = [];
100 for j=1:length(blocks)
101 bnames = [bnames cellstr(blocks(j).name)];
102 end
103 set(handles.blockList, 'String', bnames)
104
105 % get current loop
106 getLoop(handles);
107
108 % set current value for selected block/param
109 setCurrentValues(handles)
110
111 % show current block settings
112 showBlockSettings(handles)
113
114
115 %--------------------------------------------------------------------------
116 % get the current loop and set lists accordingly
117 %
118 function getLoop(handles)
119
120 myh = getappdata(handles.main, 'loopBlockHandle');
121 blocks = getappdata(handles.main, 'blocks');
122
123 set(handles.main, 'Name', get(myh, 'Name'))
124
125 %--------- Set indices for lists
126 % get mask values
127 maskVals = get(myh, 'MaskValues');
128 bname = maskVals{1};
129 % find block in list which matches the current loop settings
130 blistIdx = 1;
131 for j=1:length(blocks)
132 b = blocks(j);
133 if strcmp(b.name, bname)
134 blistIdx = j;
135 end
136 end
137 set(handles.blockList, 'Value', blistIdx)
138 % set params list
139 setParamsList(handles)
140
141 % set param to the key in loop block
142 params = get(handles.paramList, 'String');
143 param = maskVals{3};
144 pidx = 1;
145 for j=1:length(params)
146 if strcmp(params{j}, param)
147 pidx = j;
148 end
149 end
150 set(handles.paramList, 'Value', pidx);
151
152 %--------------------------------------------------------------------------
153 % set parameters list from currently selected block
154 %
155 function setParamsList(handles)
156
157 % get block handle
158 bhandle = getblockhandle(handles);
159
160 % get mask params for this block
161 maskparams = get(bhandle, 'MaskNames');
162 set(handles.paramList, 'String', maskparams);
163
164
165 % --- Outputs from this function are returned to the command line.
166 function varargout = loopgui_OutputFcn(hObject, eventdata, handles)
167 % varargout cell array for returning output args (see VARARGOUT);
168 % hObject handle to figure
169 % eventdata reserved - to be defined in a future version of MATLAB
170 % handles structure with handles and user data (see GUIDATA)
171
172 % Get default command line output from handles structure
173 varargout{1} = handles.output;
174
175
176 % --- Executes on selection change in popupmenu1.
177 function popupmenu1_Callback(hObject, eventdata, handles)
178 % hObject handle to popupmenu1 (see GCBO)
179 % eventdata reserved - to be defined in a future version of MATLAB
180 % handles structure with handles and user data (see GUIDATA)
181
182 % Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
183 % contents{get(hObject,'Value')} returns selected item from popupmenu1
184
185
186 % --- Executes during object creation, after setting all properties.
187 function popupmenu1_CreateFcn(hObject, eventdata, handles)
188 % hObject handle to popupmenu1 (see GCBO)
189 % eventdata reserved - to be defined in a future version of MATLAB
190 % handles empty - handles not created until after all CreateFcns called
191
192 % Hint: popupmenu controls usually have a white background on Windows.
193 % See ISPC and COMPUTER.
194 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
195 set(hObject,'BackgroundColor','white');
196 end
197
198
199 % --- Executes on selection change in blockList.
200 function blockList_Callback(hObject, eventdata, handles)
201 % hObject handle to blockList (see GCBO)
202 % eventdata reserved - to be defined in a future version of MATLAB
203 % handles structure with handles and user data (see GUIDATA)
204
205 % Hints: contents = get(hObject,'String') returns blockList contents as cell array
206 % contents{get(hObject,'Value')} returns selected item from blockList
207
208 set(handles.paramList, 'Value', 1);
209 setParamsList(handles)
210 setCurrentValues(handles)
211
212 % --- Executes during object creation, after setting all properties.
213 function blockList_CreateFcn(hObject, eventdata, handles)
214 % hObject handle to blockList (see GCBO)
215 % eventdata reserved - to be defined in a future version of MATLAB
216 % handles empty - handles not created until after all CreateFcns called
217
218 % Hint: listbox controls usually have a white background on Windows.
219 % See ISPC and COMPUTER.
220 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
221 set(hObject,'BackgroundColor','white');
222 end
223
224
225 % --- Executes on selection change in paramList.
226 function paramList_Callback(hObject, eventdata, handles)
227 % hObject handle to paramList (see GCBO)
228 % eventdata reserved - to be defined in a future version of MATLAB
229 % handles structure with handles and user data (see GUIDATA)
230
231 % Hints: contents = get(hObject,'String') returns paramList contents as cell array
232 % contents{get(hObject,'Value')} returns selected item from paramList
233
234 setCurrentValues(handles)
235
236 %--------------------------------------------------------------------------
237 % Set values edit box to current value
238 function setCurrentValues(handles)
239
240 param = getparam(handles);
241 bh = getblockhandle(handles);
242 blocks = getappdata(handles.main, 'blocks');
243
244 % find relevant block
245 for j=1:length(blocks)
246 b = blocks(j);
247 if b.handle == bh
248 % get maskNames
249 maskparams = get(bh, 'MaskNames');
250 for k=1:length(maskparams)
251 if strcmp(maskparams{k}, param)
252 validx = k;
253 end
254 end
255 end
256 end
257
258 % get mask values
259 maskvals = get(bh, 'MaskValues');
260 set(handles.paramValsEdit, 'String', maskvals{validx})
261
262
263 % --- Executes during object creation, after setting all properties.
264 function paramList_CreateFcn(hObject, eventdata, handles)
265 % hObject handle to paramList (see GCBO)
266 % eventdata reserved - to be defined in a future version of MATLAB
267 % handles empty - handles not created until after all CreateFcns called
268
269 % Hint: listbox controls usually have a white background on Windows.
270 % See ISPC and COMPUTER.
271 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
272 set(hObject,'BackgroundColor','white');
273 end
274
275
276
277 function paramValsEdit_Callback(hObject, eventdata, handles)
278 % hObject handle to paramValsEdit (see GCBO)
279 % eventdata reserved - to be defined in a future version of MATLAB
280 % handles structure with handles and user data (see GUIDATA)
281
282 % Hints: get(hObject,'String') returns contents of paramValsEdit as text
283 % str2double(get(hObject,'String')) returns contents of paramValsEdit as a double
284
285
286 % --- Executes during object creation, after setting all properties.
287 function paramValsEdit_CreateFcn(hObject, eventdata, handles)
288 % hObject handle to paramValsEdit (see GCBO)
289 % eventdata reserved - to be defined in a future version of MATLAB
290 % handles empty - handles not created until after all CreateFcns called
291
292 % Hint: edit controls usually have a white background on Windows.
293 % See ISPC and COMPUTER.
294 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
295 set(hObject,'BackgroundColor','white');
296 end
297
298
299 % --- Executes on button press in setLoopBtn.
300 function setLoopBtn_Callback(hObject, eventdata, handles)
301 % hObject handle to setLoopBtn (see GCBO)
302 % eventdata reserved - to be defined in a future version of MATLAB
303 % handles structure with handles and user data (see GUIDATA)
304
305 % get block
306 bh = getblockhandle(handles);
307 bname = getblockname(handles);
308
309 % get param
310 param = getparam(handles);
311
312 % get values
313 vals = cellstr(get(handles.paramValsEdit, 'String'));
314
315 level = get(handles.loopLevelSelect, 'Value');
316
317 setBlockSettings(handles, bh, bname, param, vals, 'on', level)
318
319 %--------------------------------------------------------------------------
320 % get name of currently selected param
321 function param = getparam(handles)
322
323 params = get(handles.paramList, 'String');
324 param = params{get(handles.paramList, 'Value')};
325
326
327 %--------------------------------------------------------------------------
328 % get block name of currently selected block
329 function bname = getblockname(handles)
330
331 bnames = get(handles.blockList, 'String');
332 bname = bnames{get(handles.blockList, 'Value')};
333
334 %--------------------------------------------------------------------------
335 % get block handle of currently selected block
336 function bh = getblockhandle(handles)
337
338 % get possible blocks
339 blocks = getappdata(handles.main, 'blocks');
340
341 % which block is selected
342 blist = get(handles.blockList, 'String');
343 bsel = blist{get(handles.blockList, 'Value')};
344
345 % get handle for selected block
346 for j=1:length(blocks)
347 if strcmp(blocks(j).name, bsel)
348 bh = blocks(j).handle;
349 return
350 end
351 end
352
353 %--------------------------------------------------------------------------
354 % Show current setting on GUI
355 function showBlockSettings(handles)
356
357 % get settings from block
358 myh = getappdata(handles.main, 'loopBlockHandle');
359
360 % get mask values
361 maskVals = get(myh, 'MaskValues')
362
363 % set fields on gui
364 set(handles.blockname, 'String', maskVals{1});
365 set(handles.blockhandle, 'String', maskVals{2});
366 set(handles.blockkey, 'String', maskVals{3});
367 set(handles.blockvals, 'String', maskVals{4});
368 level = str2num(maskVals{6})
369 if isempty(level)
370 level = 1;
371 end
372 if level < 1
373 level = 1;
374 end
375 if level > 5
376 level = 5;
377 end
378 set(handles.loopLevelSelect, 'value', level);
379 set(handles.blocklevel, 'String', maskVals{6});
380
381 %--------------------------------------------------------------------------
382 % Set current block settings to those selected
383 function setBlockSettings(handles, bh, bname, key, vals, state, level)
384
385 % get settings from block
386 myh = getappdata(handles.main, 'loopBlockHandle');
387
388 maskvals = {bname num2str(bh,20) key cell2str(vals) state num2str(level)};
389
390 set(myh, 'MaskValues', maskvals);
391 showBlockSettings(handles)
392
393 %--------------------------------------------------------------------------
394 % Convert a cell array to a space separated list of strings
395 function str = cell2str(c)
396
397 str = [];
398 for j=1:length(c)
399 str = [str ' ' c{j}];
400 end
401 str = strtrim(str);
402
403
404 % --- Executes on button press in getLoopBtn.
405 function getLoopBtn_Callback(hObject, eventdata, handles)
406 % hObject handle to getLoopBtn (see GCBO)
407 % eventdata reserved - to be defined in a future version of MATLAB
408 % handles structure with handles and user data (see GUIDATA)
409
410 getLoop(handles);
411
412
413 % --- Executes on selection change in loopLevelSelect.
414 function loopLevelSelect_Callback(hObject, eventdata, handles)
415 % hObject handle to loopLevelSelect (see GCBO)
416 % eventdata reserved - to be defined in a future version of MATLAB
417 % handles structure with handles and user data (see GUIDATA)
418
419 % Hints: contents = get(hObject,'String') returns loopLevelSelect contents as cell array
420 % contents{get(hObject,'Value')} returns selected item from loopLevelSelect
421
422
423 % --- Executes during object creation, after setting all properties.
424 function loopLevelSelect_CreateFcn(hObject, eventdata, handles)
425 % hObject handle to loopLevelSelect (see GCBO)
426 % eventdata reserved - to be defined in a future version of MATLAB
427 % handles empty - handles not created until after all CreateFcns called
428
429 % Hint: popupmenu controls usually have a white background on Windows.
430 % See ISPC and COMPUTER.
431 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
432 set(hObject,'BackgroundColor','white');
433 end
434
435
436 % --- Executes on selection change in loopLevelSelect.
437 function popupmenu4_Callback(hObject, eventdata, handles)
438 % hObject handle to loopLevelSelect (see GCBO)
439 % eventdata reserved - to be defined in a future version of MATLAB
440 % handles structure with handles and user data (see GUIDATA)
441
442 % Hints: contents = get(hObject,'String') returns loopLevelSelect contents as cell array
443 % contents{get(hObject,'Value')} returns selected item from loopLevelSelect
444
445
446 % --- Executes during object creation, after setting all properties.
447 function popupmenu4_CreateFcn(hObject, eventdata, handles)
448 % hObject handle to loopLevelSelect (see GCBO)
449 % eventdata reserved - to be defined in a future version of MATLAB
450 % handles empty - handles not created until after all CreateFcns called
451
452 % Hint: popupmenu controls usually have a white background on Windows.
453 % See ISPC and COMPUTER.
454 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
455 set(hObject,'BackgroundColor','white');
456 end
457
458