-
Notifications
You must be signed in to change notification settings - Fork 14
/
cjtExamples.m
421 lines (327 loc) · 12.8 KB
/
cjtExamples.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
function cjtExamples
% CJTEXAMPLES Compliant Joint Toolbox Examples GUI
%
% This file implements a graphical user interface that allows to explore
% the Matlab and Simulink examples provided with the Compliant Joint
% Toolbox.
%
% Author::
% Joern Malzahn
% Wesley Roozing
%
% See also genericJoint.
% Copyright (C) 2016, by Joern Malzahn, Wesley Roozing
%
% This file is part of the Compliant Joint Toolbox (CJT).
%
% CJT is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% CJT is distributed in the hope that it will be useful, but WITHOUT ANY
% WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
% License for more details.
%
% You should have received a copy of the GNU General Public License
% along with CJT. If not, see <http://www.gnu.org/licenses/>.
%
% For more information on the toolbox and contact to the authors visit
% <https://github.com/geez0x1/CompliantJointToolbox>
close all;
setCJTPaths;
%% Basic setup of the GUI
figname = 'Compliant Joint Toolbox Examples';
fig_props = { ...
'name' figname ...
'color' get(0,'DefaultUicontrolBackgroundColor') ...
'resize' 'off' ...
'numbertitle' 'off' ...
'menubar' 'none' ...
'windowstyle' 'normal' ...
'visible' 'on' ...
'units' 'normalized' ...
'createfcn' '' ...
'closerequestfcn' 'delete(gcbf)' ...
};
% Create main figure to use as GUI
mainFig = figure(fig_props{:});
% Create structure of handles
fig_handles = guihandles(mainFig);
%% Gater information about what examples are available
cjtPath = fileparts(which('cjtExamples'));
examplePath = [cjtPath, filesep, 'examples'];
matlabExamples = scanForExamples([examplePath, filesep, 'matlab']);
simulinkExamples = scanForExamples([examplePath, filesep, 'simulink']);
%% Some parameters to position and align uicontrols
listHeight = 0.3;
listWidth = 0.4;
listX = 0.05;
listY = 0.6;
btnY = 0.03;
btnWidth = 0.25;
%% Uicontrols for Matlab Examples
fig_handles.matlabTitle = uicontrol('Style','text',...
'units','normalized',...
'String','Matlab Examples',...
'FontWeight', 'bold',...
'FontSize', 12,...
'HorizontalAlignment', 'left',...
'Tag','matlabTitle_txt',...
'Position', [listX 0.91 listWidth 0.05],...
'Callback', {@localListbox_callback});
% Matlab examples to list
nMatlabExamples = numel(matlabExamples);
matlabExampleStrings = cell(nMatlabExamples,1);
for iEx = 1:nMatlabExamples
matlabExampleStrings{iEx,1} = matlabExamples(iEx).displayName;
end
fig_handles.matlabListbox = uicontrol('Style','listbox',...
'units','normalized',...
'String',matlabExampleStrings,...
'BackgroundColor','w',...
'Min',0,...
'Max',2,...
'Tag','matlabListbox',...
'Position',[listX listY listWidth listHeight ],...
'Callback', {@localListbox_callback});
%% Uicontrols Simulink Examples
fig_handles.simulinkTitle = uicontrol('Style','text',...
'units','normalized',...
'String','Simulink Examples',...
'FontWeight', 'bold',...
'FontSize', 12,...
'HorizontalAlignment', 'left',...
'Tag','simulinkTitle_txt',...
'Position', [0.5+listX 0.91 listWidth 0.05],...
'Callback', {@localListbox_callback});
set(fig_handles.matlabListbox, 'Value',[])
% Simulink examples to list
nSimulinkExamples = numel(simulinkExamples);
simulinkExampleStrings = cell(nSimulinkExamples,1);
for iEx = 1:nSimulinkExamples
simulinkExampleStrings{iEx,1} = simulinkExamples(iEx).displayName;
end
fig_handles.simulinkListbox = uicontrol('Style','listbox',...
'units','normalized',...
'String',simulinkExampleStrings,...
'BackgroundColor','w',...
'Min',0,...
'Max',2,...
'Tag','simulinkListbox',...
'Position',[0.5+listX listY listWidth listHeight ],...
'Callback', {@localListbox_callback});
set(fig_handles.simulinkListbox, 'Value',[])
%% Common uicontrols
% Run an example
fig_handles.run_btn = uicontrol('Style','pushbutton',...
'units','normalized',...
'String','Run Example',...
'Tag','run_btn',...
'Enable','off',...
'Position',[listX btnY btnWidth 0.05],...
'Callback',{@localRun_callback});
% Open the example m-file
fig_handles.open_btn = uicontrol('Style','pushbutton',...
'units','normalized',...
'String','Open Example',...
'Tag','open_btn',...
'Enable','off',...
'Position',[listX+btnWidth+0.05 btnY btnWidth 0.05],...
'Callback',{@localOpen_callback});
% Close the dialog
fig_handles.close_btn = uicontrol('Style','pushbutton',...
'units','normalized',...
'String','Close Dialog',...
'Tag','close_btn',...
'Position',[listX+2*(btnWidth+0.05) btnY btnWidth 0.05],...
'Callback',{@localClose_callback});
% Display the example description
descriptionString = ['Welcome to CJT. Select a Matlab or ',...
'Simulink Example and then click on the "Run" or "Open" ',...
'to start.'];
fig_handles.descriptionListbox = uicontrol('Style','listbox',...
'units','normalized',...
'String',descriptionString,...
'Tag','dListbox',...
'Enable','Off',...
'Min',0,...
'Max',2,...
'Position',[listX 0.1 0.9 0.43 ],...
'Callback', {@localDescription_callback});
set(fig_handles.descriptionListbox, 'Value',[])
fig_handles.descriptionTitle = uicontrol('Style','text',...
'units','normalized',...
'String','Example Description',...
'FontWeight', 'bold',...
'FontSize', 12,...
'HorizontalAlignment', 'left',...
'Tag','description_txt',...
'Position', [listX 0.54 listWidth 0.05]);%,...
% Extend the handles struct by some information that might be useful in
% callbacks.
fig_handles.cjtPath = cjtPath;
fig_handles.examplePath = examplePath;
fig_handles.matlabExamples = matlabExamples;
fig_handles.simulinkExamples = simulinkExamples;
fig_handles.mainFig = mainFig;
% Save the structure
guidata(mainFig,fig_handles);
end
function localDescription_callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns contents
% contents{get(hObject,'Value')} returns selected item from listbox1
% Get the structure using guidata in the local function
fig_handles = guidata(gcbo);
% Find out which example has been selected.
listboxTag = get(fig_handles.activeListbox,'Tag');
index_selected = get(fig_handles.activeListbox,'Value');
if ~isempty(index_selected)
% Get m filename
switch listboxTag
case 'matlabListbox'
mFileName = fig_handles.matlabExamples(index_selected).fileName;
case 'simulinkListbox'
mFileName = fig_handles.simulinkExamples(index_selected).fileName;
end
% Extract example description
descriptionText = extractExampleDescription(mFileName);
set(fig_handles.descriptionListbox,'string', descriptionText);
set(fig_handles.descriptionListbox, 'Value',[])
end
end
function localClose_callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns contents
% contents{get(hObject,'Value')} returns selected item from listbox1
% Get the structure using guidata in the local function
fig_handles = guidata(gcbo);
close(fig_handles.mainFig);
end
function localOpen_callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns contents
% contents{get(hObject,'Value')} returns selected item from listbox1
% Get the structure using guidata in the local function
fig_handles = guidata(gcbo);
% Find out which example has been selected.
listboxTag = get(fig_handles.activeListbox,'Tag');
index_selected = get(fig_handles.activeListbox,'Value');
% For each selected demo...
for idx = 1:numel(index_selected)
% ...get m filename and open it
switch listboxTag
case 'matlabListbox'
mFileName = fig_handles.matlabExamples(index_selected(idx)).fileName;
case 'simulinkListbox'
mFileName = fig_handles.simulinkExamples(index_selected(idx)).fileName;
end
% Open in editor
open(mFileName);
end
end
function localRun_callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns contents
% contents{get(hObject,'Value')} returns selected item from listbox1
% Get the structure using guidata in the local function
fig_handles = guidata(gcbo);
% Find out which example has been selected.
listboxTag = get(fig_handles.activeListbox,'Tag');
index_selected = get(fig_handles.activeListbox,'Value');
% For each selected demo...
for idx = 1:numel(index_selected)
% ...get m filename and run it
switch listboxTag
case 'matlabListbox'
mFileName = fig_handles.matlabExamples(index_selected(idx)).fileName;
% Run example and display results
pubPath = publish(mFileName);
web(pubPath)
case 'simulinkListbox'
mFileName = fig_handles.simulinkExamples(index_selected(idx)).fileName;
run(mFileName);
end
end
end
function localListbox_callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns contents
% contents{get(hObject,'Value')} returns selected item from listbox1
% Get the structure using guidata in the local function
fig_handles = guidata(gcbo);
% Activate Buttons
set(fig_handles.run_btn, 'Enable', 'on')
set(fig_handles.open_btn, 'Enable', 'on')
set(fig_handles.descriptionListbox, 'Enable', 'on')
% Update the active listbox property.
fig_handles.activeListbox = hObject;
% If a double-click has occurred, directly run the example.
if strcmp(get(fig_handles.mainFig,'SelectionType'),'open')
localRun_callback(hObject, [], [])
end
guidata(hObject,fig_handles);
% Update example description field.
localDescription_callback(hObject, [], []);
end
function [examples] = scanForExamples(examplePath)
% Scans for example m-files in a given directory.
%
examples = struct([]);
% Look for m-files in the directory
dirContents = dir([examplePath,filesep,'*.m']);
nFiles = numel(dirContents);
% Look into all files, look if they are valid example files and record
% information about them.
for iFile = 1:nFiles
fName = [examplePath, filesep, dirContents(iFile).name];
dispName = extractExampleDisplayName(fName);
if ~isempty(dispName) % If the m-file is actually a valid example file.
localExample.fileName = fName;
localExample.displayName = dispName;
examples = [examples, localExample];
end
end
end
function dispName = extractExampleDisplayName(fName)
% Extracts the display name of an example. The display name is indicated
% by a key string: '% #! '. This function searches for this string inside
% the m-file returns the descriptive text in the remainder of the
% corresponding text line.
%
dispName = [];
fileContents = fileread(fName);
[startIndex,endIndex] = regexp(fileContents,'% #! [^\f\n\r\t\v]*[\f\n\r\t\v]');
dispName = fileContents(startIndex+5:endIndex-1);
end
function description = extractExampleDescription(fName)
% The function collects the first commented lines in the example script.
% They should contain a meaningful description about what the example
% shows. This information is returned as a cell string.
description = {};
fid = fopen(fName);
stopFlag = 0;
cnt = 1;
while(~stopFlag)
tline = fgetl(fid);
if ~isempty(tline) && tline(1) == '%'
description{cnt,1} = tline(3:end);
else
stopFlag = 1;
end
cnt = cnt +1;
end
fclose(fid);
end