What does the class function do in MATLAB / RunMat?
class(x) returns the name of the MATLAB class that x belongs to. The result is a string scalar that you can use for diagnostics, branching, or display logic.
How does the class function behave in MATLAB / RunMat?
- Real or complex double-precision inputs (including empty numeric arrays) report
"double". Integer scalars or tensors report their narrow class such as"int16"or"uint8". - Logical scalars and arrays report
"logical". Character arrays return"char", while string scalars or arrays return"string". - Cell arrays, structs, and user-defined
classdefobjects report"cell","struct", or the class name that defined the object. - Function handles and anonymous closures both report
"function_handle". Listeners return"event.listener". gpuArrayvalues report"gpuArray"without gathering data. UseclassUnderlyingif you need the element type of the device array.- Handle objects, including deleted handles, return the class name that introduced the handle. Metadata-only values such as
classref("Point")report"meta.class".
GPU Execution Behaviour
class is an introspection-only builtin. When the input resides on the GPU, RunMat reads the residency metadata, returns the class name immediately, and leaves the data in place. No kernels are launched and no buffers are copied back to the CPU, so providers do not need to expose any hooks.
Examples of using the class function in MATLAB / RunMat
Check the class of a numeric scalar
c = class(42);
Expected output:
c = "double"
Inspect the class of a string array
names = ["Ada", "Grace", "Edsger"];
class_name = class(names);
Expected output:
class_name = "string"
Determine whether a cell array stays typed as a cell
cells = {1, "two", 3};
cell_class = class(cells);
Expected output:
cell_class = "cell"
Detect the class of gpuArray data without gathering
G = gpuArray(rand(4));
g_class = class(G);
Expected output:
g_class = "gpuArray"
Report the class name of a custom object
P = classref("Point").origin();
point_class = class(P);
Expected output:
point_class = "Point"
GPU residency in RunMat (Do I need gpuArray?)
You do not need to call gpuArray purely to query a value’s class. The auto-offload planner keeps tensors on the GPU whenever profitable, and class simply returns the residency-aware class name without altering where the data lives. Explicit gpuArray and gather calls remain available for compatibility with MATLAB code that manages residency manually.
FAQ
Does class return a string or a character array?
class returns a string scalar, matching modern MATLAB behaviour. Convert the result with char(class(...)) if you need a legacy character row vector.
How do I check whether a value is numeric?
Use isa(x, "numeric") to handle floats, complex numbers, and integers. class reports the concrete class ("double", "int32", and so on).
Will class gather gpuArray inputs?
No. RunMat inspects metadata and returns "gpuArray" immediately; buffers stay on the device.
What does class return for handle objects?
class returns the object’s defining class name (for example "MyHandleClass"). Use isa(h, "handle") to test for handle semantics.
What happens if I pass a struct or cell array?
Structs return "struct" and cell arrays return "cell", matching MATLAB.
What is the class of function handles?
Both named function handles and anonymous closures report "function_handle".
Can I distinguish string and char arrays?
Yes. class("hello") yields "string" while class('hello') yields "char".
What about metadata objects such as classref?
Calling class(classref("Point")) returns "meta.class". Use the returned meta-class object to inspect properties or superclasses.
See Also
isa, isnumeric, classref, gpuArray, gather
Source & Feedback
- The full source code for
classlives atcrates/runmat-runtime/src/builtins/introspection/class.rs. - Found a behavioural difference? Please open an issue with a minimal reproduction.