ischar — Test whether a value is a MATLAB character array (char vector or char matrix).
tf = ischar(x) returns logical true when x is a MATLAB character array (a char row vector or matrix) and logical false otherwise. Use it to distinguish traditional char arrays from the newer string type, numeric tensors, cells, or gpuArray values.
How ischar works in RunMat
- Character vectors such as
'RunMat'and char matrices created with['A'; 'B']returntrue. - Empty char arrays (including
''andchar.empty(...)) still count as character arrays. - String scalars, string arrays, cell arrays, numeric values, and logical masks all return
false. - gpuArray inputs return
falseunless they are explicitly stored as char arrays (which RunMat does not currently support), matching MATLAB’s behaviour where gpuArray does not host char data. - The result is always a host logical scalar (
logical(0)orlogical(1)), so it can be used in branching, masking, or validation code without additional conversions.
How ischar runs on the GPU
ischar never launches GPU kernels. When you pass a gpuArray value, RunMat inspects the residency metadata and simply reports false because device-resident buffers are numeric or logical. This matches MATLAB’s semantics and avoids unnecessary host↔device transfers.
GPU memory and residency
You usually do NOT need to call gpuArray yourself in RunMat (unlike MATLAB).
In RunMat, the fusion planner keeps residency on GPU in branches of fused expressions. As such, calling ischar on a gpuArray result preserves the device buffer, and ischar answers the query using metadata only.
To preserve backwards compatibility with MathWorks MATLAB, and for when you want to explicitly bootstrap GPU residency, you can call gpuArray explicitly to move data to the GPU if you want to be explicit about the residency.
Since MathWorks MATLAB does not have a fusion planner, and they kept their parallel execution toolbox separate from the core language, as their toolbox is a separate commercial product, MathWorks MATLAB users need to call gpuArray to move data to the GPU manually whereas RunMat users can rely on the fusion planner to keep data on the GPU automatically.
Examples
Checking if a character vector is a char array
tf = ischar('RunMat')Expected output:
tf = logical(1)Detecting multi-row char matrices
letters = ['ab'; 'cd'];
tf = ischar(letters)Expected output:
tf = logical(1)Distinguishing between char arrays and string scalars
tf_char = ischar('hello');
tf_string = ischar("hello")Expected output:
tf_char = logical(1)
tf_string = logical(0)Recognising that numeric and logical data are not char arrays
numbers = [1 2 3];
mask = true(1, 3);
tf_numbers = ischar(numbers);
tf_mask = ischar(mask)Expected output:
tf_numbers = logical(0)
tf_mask = logical(0)Testing gpuArray inputs
G = gpuArray(ones(2, 2));
tf_gpu = ischar(G)Expected output:
tf_gpu = logical(0)FAQ
Does ischar treat empty character arrays as char?
Yes. Both '' and arrays created with char.empty return logical(1) because they are still character arrays, even when they have zero elements.
Why does ischar return false for string scalars?
Strings in MATLAB are a distinct type introduced in R2016b. They are not interchangeable with char arrays, so ischar("text") correctly returns logical(0).
What about cell arrays of characters?
Cell arrays such as {'a', 'b'} return logical(0) because they are cell containers rather than a single char array. Use iscellstr if you need to validate cell-of-char collections.
Can gpuArray hold char data in RunMat?
Not currently. gpuArray values in RunMat contain numeric or logical tensors, so ischar reports false for all gpuArray inputs, mirroring MATLAB’s behaviour.
How do I convert a string to a char array if ischar returns false?
Use char(stringScalar) or the convertStringsToChars utility when you need to operate on char arrays for legacy APIs.
Is there any performance cost when checking large arrays?
No. ischar only inspects the value’s metadata; it does not copy or iterate over the array elements, so the cost is constant regardless of array size.
Related functions to explore
These functions work well alongside ischar. Each page has runnable examples you can try in the browser.
isa, char, string, strcmp, gpuArray, class, isstring, which, who, whos
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ischar works, line by line, in Rust.
- View ischar.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.