Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if variable x is of class 'double'.
MATLAB
if [1](x, 'double') disp('x is a double'); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'isa' to check type.
✗ Incorrect
The isa function checks if a variable is of a specified class.
2fill in blank
mediumComplete the code to get the class name of variable y.
MATLAB
typeName = [1](y); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isa' instead of 'class' to get the class name.
✗ Incorrect
The class function returns the class name of a variable as a string.
3fill in blank
hardFix the error in the code to check if z is a cell array.
MATLAB
if [1](z, 'cell') disp('z is a cell array'); end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'isa' for type checking.
✗ Incorrect
To check if a variable is a cell array, use isa with 'cell'.
4fill in blank
hardFill both blanks to create a dictionary of variable names and their classes for variables in the list.
MATLAB
vars = {'a', 'b', 'c'};
classes = containers.Map();
for i = 1:length(vars)
varName = vars{i};
classes(varName) = [1](eval([2]));
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isa' instead of 'class' for getting class name.
Using 'vars{i}' instead of 'varName'.
✗ Incorrect
Use class to get the class name of the variable whose name is stored in varName.
5fill in blank
hardFill all three blanks to check if each variable in the list is of class 'double' and store the result in a logical array.
MATLAB
vars = {x, y, z};
isDouble = false(1, length(vars));
for k = 1:length(vars)
isDouble(k) = [1](vars[2], '[3]');
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'isa' for checking type.
Using wrong indexing syntax.
✗ Incorrect
Use isa to check if each variable is 'double'. The variable is accessed as vars{k}.