MATLAB Answers

0

Does try,catch delete variables from the workspace?

Asked by Tyler Gillespie on 24 Jul 2019
Latest activity Answered by Cris LaPierre
on 27 Aug 2019 at 20:51
I am writting a code that will help with grading assignments. I know, I know, it looks terrible. If anyone has tips for optimizing it, that would be nice. Anyway, here is my code:
%This .m file will take all files in a directory and run them for
%specified test conditions. To run this code, make sure the directory
%(in line 5) is set to the location that the files are saved. Only run line
%9 if the first two entries are not file names.
clc,clear
fnames=dir('C:\Users\tyguy\Downloads\hw7'); %creates a structure from all files in directory
n=struct2cell(fnames); %converts structure of file names to cell array
names=n(1,3:end); %deletes first two entries
check=""; %creates an empty string array for files that need a manual check
pass=""; %creates an empty string array for files that pass
pauseforchecking=0; %change to 1 if you want to pause between files
opencheckfiles=0; %change to 1 if you want to open files that need checking
for p=1:length(names) %iterates through every file name
fnam=char(names(p)); %saves file name to a char variable for indexing
if fnam(end-2)=='1' %checks if problem 1
clc,clearvars -except fnam names check pass pauseforchecking opencheckfiles
%input test conditions here
x=[3 2 4 27 3 1 10]; sol=max(x);
%tries to run code or catch an error
try
err=0; %no error
run(fnam(1:end-2)) %runs problem 1
catch
check=[check;fnam]; %saves current file to check
err=1; %error has occured
end
%checks answer against solution
if exist('y','var')==1 %checks if outputs exist
if isequal(y,sol) && err~=1
pf='Pass!';
pass=[pass;fnam]; %saves file name to pass
else
pf='Fail!';
check=[check;fnam]; %saves current file name to check
end
%outputs results
fprintf('\nFile Name: %s\n',fnam)
y
fprintf('%s\n\n',pf)
else
%outputs results
fprintf('\nFile Name: %s\n',fnam)
warning('Output does not exist')
end
if pauseforchecking==1
input('Press Enter to Proceed..') %pauses execution to check result
end
elseif fnam(end-2)=='2' %checks if problem 2
clc,clearvars -except fnam names check pass pauseforchecking opencheckfiles
%input test conditions here
x=[0 1 0 0 1 0]; [isol,jsol]=find(x);
%tries to run code or catch an error
try
err=0; %no error
run(fnam(1:end-2)) %runs problem 2
catch
check=[check;fnam]; %saves current file name to check
err=1; %error has occured
end
%checks answer against solution
if exist('i','var')== 1 && exist('j','var')==1 %checks if outputs exist
if isequal(i,isol) && isequal(j,jsol) && err~=1
pf='Pass!';
pass=[pass;fnam]; %saves file name to pass
else
pf='Fail!';
check=[check;fnam]; %saves current file name to check
end
%outputs results
fprintf('\nFile Name: %s\n',fnam)
i
j
fprintf('%s\n\n',pf)
else
%outputs results
fprintf('\nFile Name: %s\n',fnam)
warning('Output does not exist')
end
if pauseforchecking==1
input('Press Enter to Proceed..') %pauses execution to check result
end
elseif fnam(end-2)=='3' %checks if problem 3
clc,clearvars -except fnam names check pass pauseforchecking opencheckfiles
%input test conditions here
x=[1 5 2 3 8 4 1 0]; sol=sort(x);
try
err=0; %no error
run(fnam(1:end-2)) %runs problem 3
catch
check=[check;fnam]; %saves current file name to check
err=1; %error has occured
end
%checks answer against solution
if exist('y','var')~=0 %checks if outputs exist
if isequal(y,sol) && err~=1
pf='Pass!';
pass=[pass;fnam]; %saves file name to pass
else
pf='Fail!';
check=[check;fnam]; %saves current file name to check
end
%outputs results
fprintf('\nFile Name: %s\n',fnam)
y
fprintf('%s\n\n',pf)
else
%outputs results
fprintf('\nFile Name: %s\n',fnam)
warning('Output does not exist')
end
if pauseforchecking==1
input('Press Enter to Proceed..') %pauses execution to check result
end
end
end
clc,clearvars -except pass check names
check=cellstr(check(2:end,:)); pass=cellstr(pass(2:end,:)); %converts pass and check back to cell arrays
%opens files that need to be checked
if opencheckfile == 1
for i=1:length(check)
c=['C:\Users\tyguy\Downloads\hw7\',check(i)]; %creates array of file location and name
c=[c{:}]; %combines location and name into one element
edit(c) %opens file in editor
end
end
I am getting an error in line 133 (where I add a student's filename to an array of files I need to manually check). The variable "check" seems to disappear along with all other variables and it gives me an error saying it doesn't exist. I can't see where it could possibly be clearing the "check" variable. I thought maybe when catch happens, the workspace is cleared. I can't find any documentation or answers talking about catch clearing the workspace. The problem only happens if running the file produces an error (so when it runs the catch line). Another thing to add is that this code ran just fine, but when I opened it again, it gave me this error. I'm not sure why it would work before, then stop working when reopened.

  4 Comments

Show 1 older comment
Ah! Such an obvious solution. Thank you very much. One file had a clear all in there.
You should revise your code:
1. Clearing variables in code is rarely required in well-written code. If you avoid meta-programming, such as clearing variables and checking for the existence of variables, then most likely you could make your code simpler, more robust, and more efficient.
2. There is no point in converting to a cell array and duplicating that data:
n=struct2cell(fnames); %converts structure of file names to cell array
you can simply access the structure data directly, e.g.
fnames(k).name
3. Although common, this is a bug in your code:
names=n(1,3:end); %deletes first two entries
because there is no reason that the folder aliases '.' and '..' will always be the first two results returned. In fact it is trivial to think of some folder names where those are not the first two folder names returned:
>> dir('*')
&a (a) +a -a . .. aa
% Are these the first two results returned?: ^ ^^ (hint: no)
You should use ismember or setdiff to remove those folder names.
Thanks for the advice! I will definitely impliment this into my code. The reason I check for existence is because the instructions were to name the output y and some students used different variable names or just never finished the problem, which would produce an error and end the code.

Sign in to comment.

Tags

1 Answer

Answer by Cris LaPierre
on 27 Aug 2019 at 20:51

I'm curious if you are aware of MATLAB Grader. This could potentially spare you a lot of work.

  0 Comments

Sign in to comment.