strings — Preallocate string arrays filled with empty string scalars using MATLAB-compatible size syntax.
strings creates string arrays populated with empty string scalars (""). It supports MATLAB-compatible scalar, vector, and variadic dimension forms for preallocation.
Syntax
S = strings()
S = strings(sz)
S = strings(m, n...)
S = strings(___, "like", p)
S = strings(___, fill)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
sz | SizeArg | Yes | — | Size vector or scalar side length. |
m | SizeArg | Yes | — | First array dimension. |
n... | SizeArg | Variadic | — | Additional array dimensions. |
dims... | SizeArg | Variadic | — | Optional explicit size dimensions. |
like | StringScalar | Yes | "like" | Literal option keyword "like". |
p | LikePrototype | Yes | — | Prototype value supplying output shape when dims are omitted. |
fill | StringScalar | Yes | "empty" | Fill mode keyword: "empty" or "missing". |
Returns
| Name | Type | Description |
|---|---|---|
S | Any | Preallocated string array. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:strings:InvalidSize | Size arguments are not valid numeric scalar/vector dimensions. | strings: size arguments must be numeric scalars or vectors |
RunMat:strings:LikeMissingPrototype | "like" is provided without a following prototype. | strings: expected prototype after 'like' |
RunMat:strings:LikeDuplicate | Multiple "like" options are supplied in one call. | strings: multiple 'like' specifications are not supported |
RunMat:strings:SizeOverflow | Requested dimensions overflow platform limits. | strings: requested size exceeds platform limits |
RunMat:strings:InternalError | Internal string array construction failed. | strings: internal error |
How strings works
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.
Does RunMat run strings on the GPU?
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
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 stringPreserving trailing singleton dimensions
column = strings(3, 1, 1, 1);
sz = size(column)Expected output:
sz =
3 1 1 1Filling 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)
endExpected output:
Error using strings
Size inputs must be nonnegative integers.Using strings with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how strings changes the result.
Run a small strings example, explain the result, then change one input and compare the output.
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.
Related Strings functions
Core
char · compose · num2str · sprintf · str2double · strcmp · strcmpi · string · string.empty · strlength · strncmp
Search
contains · endsWith · startsWith · strfind
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strings is executed, line by line, in Rust.
- View the source for strings in Rust on GitHub
- Learn how the RunMat 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 blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.