Consider the following MATLAB commands executed in order:
clear all
x = 5;
y = 10;
clear x;
z = exist('x', 'var');What is the value of z after running this code?
clear all x = 5; y = 10; clear x; z = exist('x', 'var');
Recall that clear x removes variable x from the workspace.
After clear x, variable x no longer exists in the workspace. The exist function returns 0 when the variable is not found.
Given the following MATLAB commands:
a = 1; b = 2; c = 3; clearvars -except a c;
How many variables are in the workspace after these commands?
a = 1; b = 2; c = 3; clearvars -except a c;
The clearvars -except command keeps only specified variables.
Only variables a and c remain after clearing all except these two.
What error will this MATLAB code produce?
clear all
x = 10;
if exist('x')
disp('x exists');
endclear all x = 10; if exist('x') disp('x exists'); end
exist('x') without a second argument is valid and returns 1 for variables in the workspace.
No error occurs. exist('x') returns 1 because x is a variable in the workspace, so 'x exists' is printed.
Consider this MATLAB code:
a = 1; b = 2; c = 3; clear a b vars = who;
What does vars contain after running this code?
a = 1; b = 2; c = 3; clear a b vars = who;
The clear command removes specified variables from the workspace.
After clearing a and b, only c remains. The who function lists current variables.
You have variables data1, data2, temp in your workspace. Which MATLAB command saves only variables starting with 'data' to myfile.mat?
Use the -regexp option to save variables matching a pattern.
Option C uses -regexp '^data' to save all variables starting with 'data'. Option C is invalid syntax. Option C requires listing all variables explicitly. Option C is invalid because -except is not a valid save option.