What does the strings function do in MATLAB / RunMat?
strings creates string arrays whose elements are empty string scalars (""). It mirrors MATLAB's
preallocation helper, accepting scalar, vector, or multiple dimension arguments to control the
array shape.
How does the strings function behave in MATLAB / RunMat?
stringswith no inputs returns a 1×1 string array containing"".strings(n)produces ann-by-narray of empty strings. The single input must be a nonnegative integer scalar.strings(sz1,...,szN)andstrings(sz)accept nonnegative integer sizes. All specified dimensions—including trailing singletons—are preserved in the resulting array.- Setting any dimension to
0yields an empty array whose remaining dimensions still shape the result (for example,strings(0, 5, 3)is a0×5×3string array). strings(___, "missing")fills the allocation with the missing sentinel (<missing>) instead of empty strings, which is useful when you plan to replace placeholders later.strings(___, "like", prototype)orstrings("like", prototype)reuses the size ofprototypewhen you omit explicit dimensions. Any provided dimensions still take precedence, and GPU prototypes are gathered before their shape is inspected.- Size inputs must be finite integers. Negative, fractional, or NaN values trigger MATLAB-compatible "Size inputs must be nonnegative integers" errors.
- Only numeric or logical size arguments are supported. Other types (strings, structs, objects) raise descriptive errors.
strings Function GPU Execution Behaviour
strings never allocates data on the GPU. Size arguments that reside on a GPU are automatically
gathered to the host before validation, and the resulting string array always lives in host memory.
"like" prototypes follow the same rule—they are gathered before their shape is inspected. No
provider hooks are required, so the GPU metadata marks the builtin as a gather-only operation.
Examples of using the strings function in MATLAB / RunMat
Creating a square array of empty strings
S = strings(4);
Expected output:
S = 4x4 string
"" "" "" ""
"" "" "" ""
"" "" "" ""
"" "" "" ""
Preallocating with separate dimension arguments
grid = strings(2, 3, 4);
Expected output:
grid = 2x3x4 string
grid(:,:,1) =
"" "" ""
"" "" ""
Cloning the size of another array
A = magic(3);
placeholders = strings(size(A));
Expected output:
placeholders = 3x3 string
"" "" ""
"" "" ""
"" "" ""
Handling zero dimensions
emptyRow = strings(0, 5);
Expected output:
emptyRow = 0x5 string
Preserving trailing singleton dimensions
column = strings(3, 1, 1, 1);
sz = size(column);
Expected output:
sz =
3 1 1 1
Filling arrays with missing string scalars
placeholders = strings(2, 3, "missing");
Expected output:
placeholders = 2x3 string
<missing> <missing> <missing>
<missing> <missing> <missing>
Matching an existing array with 'like'
proto = zeros(3, 2);
labels = strings("like", proto);
Expected output:
labels = 3x2 string
"" ""
"" ""
"" ""
Validating size inputs
try
strings(-3);
catch ME
disp(ME.message)
end
Expected output:
Error using strings
Size inputs must be nonnegative integers.
FAQ
How is strings different from string?
strings preallocates empty string scalars, while string converts existing data to string
scalars. Use strings to reserve space, then assign values later.
Can I use non-integer sizes such as 2.5?
No. All size arguments must be finite integers. Fractional or NaN values raise descriptive errors.
How do I create missing string values (<missing>)?
Pass "missing" as an option— for example strings(2, 3, "missing") produces a 2×3 array filled
with <missing> placeholders. You can still assign values later to replace the sentinel.
How can I reuse the size of an existing array?
Provide the "like" option: strings("like", prototype) copies the size of prototype when you do
not supply explicit dimensions. Any dimensions you specify override the inferred size.
Does the output ever live on the GPU?
No. strings always returns a host-resident string array. GPU inputs supplying sizes are gathered
before validation.
How can I create a row versus column vector?
Use strings(1, n) for a row and strings(n, 1) for a column. Additional dimensions—including trailing singletons—remain part of the array shape.
Can I pass non-numeric types such as structs or string arrays as size inputs?
No. Only numeric or logical values are accepted. Other types produce MATLAB-compatible usage errors.
Is there an equivalent to string.empty?
Yes. strings(0) returns the same 0-by-0 empty string array as string.empty.
See Also
string, char, zeros, string.empty