HOWTO · MATLAB
How to Check Variable Type in MATLAB
In this article, we'll explore different methods to check the type of a given variable in MATLAB.
On this page
To check a MATLAB value, use class() to retrieve its class name, whos to inspect variables in the current workspace, and predicates such as isa(), ischar(), or isnumeric() to get a logical result for a condition. Choose the method that matches whether you need to identify one value, review workspace variables, or guide a branch in your code.
Check a Variable’s Type with class() in MATLAB
The class() function returns a character vector naming a value’s class. For example, MATLAB treats the numeric literal 42 as a double and the single-quoted text 'Hello, MATLAB!' as a character array with class char. This Octave-compatible script shows the inspection and predicates in one workflow.
numericVar = 42;
charVar = 'Hello, MATLAB!';
fprintf('numericVar class: %s\n', class(numericVar));
fprintf('charVar class: %s\n', class(charVar));
fprintf('numericVar is double: %d\n', isa(numericVar, 'double'));
fprintf('charVar is a character array: %d\n', ischar(charVar));
fprintf('numericVar is numeric: %d\n', isnumeric(numericVar));
if isa(numericVar, 'double')
fprintf('Process numericVar as a double.\n');
end
numericVar class: double
charVar class: char
numericVar is double: 1
charVar is a character array: 1
numericVar is numeric: 1
Process numericVar as a double.
In MATLAB, use class(value) when you need the actual class name; this call reports one value and does not inventory the workspace.
Inspect Variable Types with the whos Command in MATLAB
The whos command summarizes variables in the current workspace, including their names, sizes, byte counts, classes, and attributes. Enter whos to see the workspace inventory, or use whos numericVar to inspect one named variable. The class column helps you review several values together. Its view is limited to variables visible in the current workspace or scope, so it cannot inspect an arbitrary expression that has not been assigned to a variable. Use class(expression) for that single-value question.
Test for a Specific Type with isa() in MATLAB
Use isa(value, 'typeName') when your code needs to know whether a value belongs to a requested class. With numericVar = 42 and charVar = 'Hello, MATLAB!', isa(numericVar, 'double') and isa(charVar, 'char') return logical true values, while isa(numericVar, 'single') returns false. The result can directly control an if statement, as in the script above. isa() also recognizes MATLAB class relationships, so it may return true for a superclass or interface even when that name is not the value’s exact class. Use class(value) to retrieve the class name itself.
Check for Character Arrays with ischar() in MATLAB
The ischar() function returns a logical value indicating whether its input is a character array. For charVar = 'Hello, MATLAB!', ischar(charVar) returns true; ischar(42) returns false. You can use if ischar(value) to select text handling when a function expects character-array input. MATLAB string arrays, written with double quotes, are a distinct type and do not pass this character-array check. This distinction helps when a program accepts both character vectors and string values.
Check for Numeric Values with isnumeric() in MATLAB
Use isnumeric(value) to test whether a value has a numeric type. For the examples above, isnumeric(numericVar) returns true and isnumeric(charVar) returns false. The predicate includes MATLAB numeric classes such as double, single, and the integer classes; it does not say that an accepted value is specifically a double. Logical values are not numeric for this predicate. When a particular class is required, test it with isa(value, 'double') or inspect the exact name with class(value).
Conclusion
Use class() to retrieve one value’s class name, whos to review variables in the current workspace, and isa(), ischar(), or isnumeric() to get a logical condition for your code. These tools answer different questions: inspect with class() or whos, and choose the predicate that matches the requirement your program needs to enforce.