-
Notifications
You must be signed in to change notification settings - Fork 1
/
tutorial.m
188 lines (171 loc) · 7.72 KB
/
tutorial.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
function tutorial
%TUTORIAL An interactive tutorial to teach people how to use basic MATLAB
%functionality
% The user just needs to execute the function and answer the questions by
% filling in MATLAB commands. First a brief background on each topic is
% provided, followed by a question. The function waits for input from the
% user and checks if the answer is correct. This is done in form of
% string comparison.
%
% The background, questions and possible answers are all stored in a cell
% inside the 'tasks.mat' file. The progress for the questions is saved in
% the 'progress.mat' file and multiple users are supported. The
% progress.mat file is not part of the git repository so that the
% progress is not erased when pull updates from the server.
% Loading the matlab structure that contains the tasks for the tutorial
load tasks.mat
load messages.mat
gong = load('gong');
handel = load('handel');
% Checking to see if a progress file exists. If not, then it will be
% created later on
if exist('progress.mat','file')
load progress.mat
else
progress = struct;
end
% Getting the username, for storing progress of a student
user = input('Please provide your username (no numbers please): ','s');
courses = {'Robotics','Optimisation','System identification'};
figure('Position',[300 300 200 100],'Name','Selection','MenuBar','none');
parentpanel = uipanel('Units','pixels','Title','Choose your course','Position',[5 5 190 90]);
lbs = uicontrol ('Parent',parentpanel,'Style','popupmenu','String',courses,'Units','Pixels','Position',[4 5 180 60]);
uicontrol('Style','pushbutton','String','Done','Position',[50 10 100 30],'Callback',@hDoneCallback);
uiwait
course = courses(get(lbs,'Value'));
close
users = fieldnames(progress);
sound(gong.y,gong.Fs)
% Getting a list of questions
questions = fieldnames(tasks);
% Checking if the user exists. If not, then create her
if ~ismember(user,users)
progress.(user) = 1;
end
level = progress.(user);
repeat = 0;
% Loop over all the questions
while level<=length(questions)
task = tasks.(questions{level});
if ismember(lower(course),task.courses) || ismember('all',task.courses)
if ~repeat
fprintf('Background:\n=====================================================\n\n')
% Print the background information
fprintf([task.background,'\n'])
fprintf('=====================================================\n\n')
end
% Providing the option to go back one question and practise
% again
fprintf('Type ''previous'' for going one question back, ''background'' for reading the background again, or exit to leave the tutorial\n\n')
% Print the question and wait for input from the user
answer = input(['Question ',num2str(level),':\n\n',task.question,': '],'s');
% Going back a level, if the user wants to
switch answer
case 'previous'
level = max(level-1,1);
continue
case 'exit'
return
case 'background'
repeat = 0;
continue
case 'ronco'
level = level+1;
continue
end
% Pre-evaluation events, in case some workspace preparation is
% needed
for command=task.preeval
try
eval([command{:},';'])
catch e
fprintf(2,'MATLAB error message:\n%s \n',e.message);
fname=e.stack.name;
if ~(strcmp(fname,'tutorial')==1)
l=e.stack.line;
fprintf(2,'Error in %s.m file, at line %d\n',fname, l);
end
end
end
switch task.type
case 'string'
% Check if the input matches the possible answers registered. If
% yes, then print a congratulatory message and update the progress.
% If not then ask the question again
correct = find(ismember(task.evaluation, answer));
case 'commands'
% Do several evaluations to check if the code is behaving
% the way it should. The evaluations should all give a 1 if
% they are successful or 0 if they are not. If a 0 is
% generated the evaluation sequence is stopping.
correct = 1;
command = 1;
try
eval(answer)
catch e
fprintf(2,'MATLAB error message:\n%s \n',e.message);
fname=e.stack.name;
if ~(strcmp(fname,'tutorial')==1)
l=e.stack.line;
fprintf(2,'Error in %s.m file, at line %d\n',fname, l);
end
correct = 0;
end
while (command<=length(task.evaluation) && correct>0)
try
correct = eval(task.evaluation{command})*correct;
command = command+1;
catch e
fprintf(2,'MATLAB error message:\n%s \n',e.message);
fname=e.stack.name;
if ~(strcmp(fname,'tutorial')==1)
l=e.stack.line;
fprintf(2,'Error in %s.m file, at line %d\n',fname, l);
end
correct = 0;
break
end
end
end
% Post-evaluation events, in case some cleaning-up is required
for command=task.posteval
try
eval([command{:},';'])
catch e
fprintf(2,'MATLAB error message:\n%s \n',e.message);
fname=e.stack.name;
if ~(strcmp(fname,'tutorial')==1)
l=e.stack.line;
fprintf(2,'Error in %s.m file, at line %d\n',fname, l);
end
end
end
if all(correct)
cmessage = randperm(numel(congrats)); %#ok<USENS> Loaded at the beginning of the function
fprintf([congrats{cmessage(1)},'\n\nPress enter to continue\n\n'])
level = level+1;
progress.(user) = level;
save('progress.mat','progress')
repeat = 0;
pause
else
rmessage = randperm(numel(retry)); %#ok<USENS> Loaded at the beginning of the function
fprintf([retry{rmessage(1)},'\n\n'])
repeat = 1;
end
else
level = level+1;
repeat = 0;
end
end
% When all questions are completed, then congratulate the user and
% direct her to the github repository.
sound(handel.y,handel.Fs)
fprintf(['\n\nWell done! You have completed all the questions!\nYou are now ready to dive deeper into MATLAB and become a guru one day!\n',...
'We hope you enjoyed. You can find updates of this tutorial on our github repository https://www.github.com/tassos/matlab_tutorial\n\n'])
end
%% Resume
% Callback to resume operation after user has made her choices
function hDoneCallback(~, ~)
uiresume
end