% % Provides MATLAB practice by reading commands from % disp('Welcome to the COEN 45 tutor.'); disp('Practice problems are taken from practice.m which you can change.') disp('Only questions with one-line responses will work.') disp('Type "error" (without the quotes) if you expect an error.') disp(''); pause(2) practice_script = 'practice.m'; [fid,message] = fopen(practice_script); error(message); praise = 'Good work!'; command = fgetl(fid); % % "command" will be -1 at end-of-file % while ischar(command) % % skip comments % while strcmp(command(1),'%') | isempty(command) command = fgetl(fid); end disp(command); if findstr(command,'pause') response = input('What will MATLAB display?\n','s'); try matlab = evalc(command); catch matlab = 'error'; end neq = findstr(matlab,'='); if ~isempty(neq) matlab = matlab(neq+1:end); end response(find(isspace(response))) = []; neq = findstr(response,'='); if ~isempty(neq) response = response(neq+1:end); end raw = matlab; matlab(find(isspace(matlab))) = []; while ~strcmp(response,matlab) response = input('Sorry, try again or ? to give up\n','s') giveup = strcmp(response(1),'?'); if giveup disp(raw) praise = []; break else response(find(isspace(response))) = []; neq = findstr(response,'='); if ~isempty(neq) response = response(neq+1:end); end praise = 'Good work!'; end end disp(praise) praise = 'Good Work!'; pause(0.5) else eval(command); end command = fgetl(fid); end