Bird
Raised Fist0
MATLABdata~10 mins

Type checking (class, isa) in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Type checking (class, isa)
Start with variable
↓
Check type with class()
↓
Compare class string
↓
Match
↓
Do action
↓
Start with variable
↓
Check type with isa()
↓
isa returns true or false
↓
True
↓
Do action
First, get the variable's type using class() or check type inheritance with isa(). Then decide what to do based on the result.
Execution Sample
MATLAB
x = 5;
if strcmp(class(x), 'double')
    disp('x is a double')
end

if isa(x, 'double')
    disp('x is a double (isa)')
end
This code checks if variable x is of type double using class() and isa(), then prints messages.
Execution Table
StepActionEvaluationResultOutput
1Assign x = 5x = 5x is double (numeric)
2Evaluate class(x)class(5)'double'
3Compare class(x) == 'double'strcmp(class(x), 'double')true
4If true, disp('x is a double')truedisplay messagex is a double
5Evaluate isa(x, 'double')isa(5, 'double')true
6If true, disp('x is a double (isa)')truedisplay messagex is a double (isa)
7EndNo more codeExecution stops
💡 Reached end of code, all checks done
Variable Tracker
VariableStartAfter Step 1After Step 7
xundefined55
Key Moments - 2 Insights
Why do we use strcmp with class(x) instead of ==?
Because class(x) returns a string, and in MATLAB strings must be compared with strcmp, not ==. See execution_table row 3.
What is the difference between class() and isa()?
class() returns the exact type name as a string, while isa() checks if the variable is of a given type or inherits from it. See execution_table rows 2 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 4?
Ax is a double (isa)
Bx is a double
CNo output
DError
💡 Hint
Check the Output column at step 4 in the execution_table
At which step does isa(x, 'double') get evaluated?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look at the Action column for isa() evaluation in the execution_table
If x was a string 'hello', what would strcmp(class(x), 'double') return?
Aerror
Btrue
Cfalse
Ddepends on isa
💡 Hint
Recall class('hello') returns 'char', so strcmp('char', 'double') is false
Concept Snapshot
Type checking in MATLAB:
- Use class(var) to get exact type as string
- Compare with strcmp for equality
- Use isa(var, 'TypeName') to check type or inheritance
- isa returns true/false, class returns string
- Use these to control program flow based on variable type
Full Transcript
This example shows how MATLAB checks variable types using class() and isa(). First, variable x is assigned the value 5. Then class(x) returns 'double', which is compared to the string 'double' using strcmp. Since they match, the program prints 'x is a double'. Next, isa(x, 'double') checks if x is of type double or inherits from it, returning true, so it prints 'x is a double (isa)'. The execution table traces each step, showing variable values and outputs. Key points include using strcmp for string comparison and understanding the difference between class and isa. The visual quiz tests understanding of outputs and evaluation steps. This helps beginners see how MATLAB checks types step-by-step.

Practice

(1/5)
1. What does the class function in MATLAB return when used on a variable?
easy
A. True if the variable is numeric
B. The exact type name of the variable
C. The size of the variable
D. The value of the variable

Solution

  1. Step 1: Understand the purpose of class

    The class function returns the exact type name of the variable, such as 'double', 'char', or a user-defined class. Other choices describe checks for numeric type, size, or value, but class specifically returns the type name as a string.
  2. Final Answer:

    The exact type name of the variable -> Option B
  3. Quick Check:

    class = exact type name [OK]
Hint: Remember: class gives type name as text [OK]
Common Mistakes:
  • Confusing class with isa function
  • Thinking class returns size or value
  • Assuming class returns a boolean
2. Which of the following is the correct syntax to check if variable x is of class double using isa?
easy
A. class(x) == 'double'
B. isa('double', x)
C. isa(x, 'double')
D. class(x, 'double')

Solution

  1. Step 1: Recall isa function syntax

    The correct syntax is isa(variable, 'classname'), so to check if x is a double, use isa(x, 'double'). Using class(x) == 'double' compares strings unreliably, isa('double', x) reverses arguments, and class(x, 'double') has invalid syntax.
  2. Final Answer:

    isa(x, 'double') -> Option C
  3. Quick Check:

    isa(variable, 'class') syntax [OK]
Hint: isa(variable, 'classname') checks type safely [OK]
Common Mistakes:
  • Swapping arguments in isa
  • Using == to compare class strings
  • Calling class with two arguments
3. What is the output of the following MATLAB code?
x = 5; y = isa(x, 'double'); disp(y);
medium
A. 'double'
B. 0
C. Error
D. 1

Solution

  1. Step 1: Trace code execution

    Variable x is assigned 5, which defaults to class double in MATLAB. isa(x, 'double') returns logical true (displayed as 1), and disp(y) outputs 1.
  2. Final Answer:

    1 -> Option D
  3. Quick Check:

    isa(5, 'double') = 1 [OK]
Hint: Numeric literals are double by default [OK]
Common Mistakes:
  • Expecting 0 because 5 looks like int
  • Confusing output with class function
  • Thinking isa returns string
4. Identify the error in this MATLAB code snippet:
if isa('double', x)
    disp('x is double')
end
medium
A. Arguments of isa are reversed
B. Missing semicolon after disp
C. isa cannot be used inside if
D. No error, code is correct

Solution

  1. Step 1: Check isa function argument order

    The correct order is isa(variable, 'classname'), but here arguments are reversed to isa('double', x). A missing semicolon after disp or using isa in if are not errors.
  2. Final Answer:

    Arguments of isa are reversed -> Option A
  3. Quick Check:

    isa(variable, 'class') order matters [OK]
Hint: Always put variable first in isa() [OK]
Common Mistakes:
  • Swapping arguments in isa
  • Thinking missing semicolon causes error
  • Believing isa can't be used in if
5. You want to write a MATLAB function that accepts either a numeric array or a table. Which code snippet correctly uses isa to check the input type?
hard
A. if isa(input, 'double') || isa(input, 'table')
B. if isa(input, 'array') || isa(input, 'table')
C. if class(input) == 'numeric' || class(input) == 'table'
D. if isa(input, 'numeric') || isa(input, 'table')

Solution

  1. Step 1: Identify correct class names and syntax

    In MATLAB, numeric arrays usually have class 'double', not 'numeric' or 'array'. if isa(input, 'double') || isa(input, 'table') correctly checks either using proper isa syntax and logical OR.
  2. Final Answer:

    if isa(input, 'double') || isa(input, 'table') -> Option A
  3. Quick Check:

    Use isa with correct class names [OK]
Hint: Use isa with exact MATLAB class names [OK]
Common Mistakes:
  • Using 'numeric' instead of 'double'
  • Using class() with == for strings
  • Assuming 'array' is a class name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard