What does the isstring function do in MATLAB / RunMat?
tf = isstring(x) returns a logical scalar true when x is a MATLAB string array (including
string scalars and empty string arrays) and logical false otherwise. Use it when you need to
validate arguments, branch on string support, or distinguish strings from other text
representations.
How does the isstring function behave in MATLAB / RunMat?
- String scalars such as
"RunMat"and string arrays like["alpha", "beta"]both returnlogical(1). - Empty string arrays produced by
strings(0)orstring.empty(...)are still recognised as the string type, so they returnlogical(1). - Character arrays created with single quotes (
'text'), cell arrays of character vectors, numeric tensors, logical masks, structs, tables, and other non-string containers returnlogical(0). - gpuArray inputs currently store numeric or logical tensors in RunMat, so
isstring(gpuArray(...))returnslogical(0), matching MATLAB’s behaviour. - The result is always a host logical scalar, making it easy to use for argument validation, control-flow guards, or input parsing.
isstring Function GPU Execution Behaviour
isstring runs entirely from metadata. When you pass a gpuArray, RunMat inspects the handle and
returns logical(0) because device buffers do not yet store string data. No kernels are launched,
and no host↔device transfers occur, so the device allocation remains resident for subsequent
operations.
GPU residency in RunMat (Do I need gpuArray?)
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 isstring on a gpuArray result leaves the device allocation untouched while still answering
the question 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 of using the isstring function in MATLAB / RunMat
Checking if a string scalar is recognised
tf = isstring("RunMat");
Expected output:
tf = logical(1)
Detecting string arrays with multiple elements
values = ["alpha", "beta", "gamma"];
tf = isstring(values);
Expected output:
tf = logical(1)
Distinguishing strings from character vectors
tf_string = isstring("hello");
tf_char = isstring('hello');
Expected output:
tf_string = logical(1)
tf_char = logical(0)
Confirming that empty string arrays are still strings
emptyStrs = strings(0, 2);
tf = isstring(emptyStrs);
Expected output:
tf = logical(1)
Recognising that numeric and logical arrays are not strings
numbers = [1 2 3];
mask = true(1, 3);
tf_numbers = isstring(numbers);
tf_mask = isstring(mask);
Expected output:
tf_numbers = logical(0)
tf_mask = logical(0)
Validating cell arrays of character vectors
cells = {'a', 'b', 'c'};
tf = isstring(cells);
Expected output:
tf = logical(0)
Inspecting gpuArray inputs without transfers
G = gpuArray(ones(2, 2));
tf = isstring(G);
Expected output:
tf = logical(0)
Using isstring in argument validation
function greet(name)
if ~isstring(name)
error("Name must be provided as a string.");
end
disp("Hello " + name + "!");
end
greet("RunMat");
% greet(42); % throws "Name must be provided as a string."
Expected output:
Hello RunMat!
FAQ
Does isstring return true for empty string arrays?
Yes. Empty string arrays are still of type string, so isstring(strings(0)) returns logical(1).
How is isstring different from ischar?
isstring recognises MATLAB’s modern string type introduced in R2016b. ischar recognises legacy
character arrays created with single quotes. Only isstring returns true for double-quoted
values.
What about string arrays stored inside cell arrays?
Cell arrays are containers, so isstring({'a', 'b'}) returns logical(0). Use iscellstr or
all(isstring(cells)) to validate the contents instead.
Can gpuArray hold string data in RunMat?
Not currently. gpuArray values hold numeric or logical tensors, so isstring always reports
logical(0) for device-resident data.
Does isstring inspect array contents element by element?
No. It only checks the value’s type metadata, so the cost is constant regardless of array size.
See Also
ischar, isa, string, convertStringsToChars, gpuArray
Source & Feedback
- The full source code for the implementation of the
isstringfunction is available at:crates/runmat-runtime/src/builtins/introspection/isstring.rs - Found a bug or behavioral difference? Please open an issue with details and a minimal repro.