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. Double, single, and all eight integer classes are accepted for the integer-valued size.strings(sz1,...,szN)andstrings(sz)accept finite integer sizes. Negative values are treated as zero, and trailing dimensions of size one beyond the second dimension are ignored.- 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). - The
"missing"or"empty"fill option is a mode-gated RunMat extension; the documentedstringscontract creates empty strings. - The
"like"prototype form is a mode-gated RunMat extension. It inspects only prototype shape and does not convert an integer prototype payload. - Fractional, infinite, NaN, or platform-unrepresentable sizes reject before allocation; negative finite integer values become zero.
- 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 string data on the GPU. Documented GPU-array size values and automatically resident size intermediates gather through their owning provider before exact validation. The RunMat-only "like" form follows the same host allocation rule.
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 stringIgnoring trailing singleton dimensions beyond the second
column = strings(3, 1, 1, 1);
sz = size(column)Expected output:
sz =
3 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's shape
proto = zeros(3, 2);
labels = strings("like", proto)Expected output:
labels = 3x2 string
"" ""
"" ""
"" ""Treating a negative size as zero
emptySquare = strings(-3)Expected output:
emptySquare = 0x0 stringUsing 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>)?⌄
In runmat mode, pass "missing" as an extension option—for example, strings(2, 3, "missing"). MATLAB-compatible mode accepts the documented empty-string construction forms only.
How can I reuse the size of an existing array?⌄
RunMat mode provides strings("like", prototype) as an extension. For portable source, use strings(size(prototype)).
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. Dimensions beyond the second are retained when they are not one; trailing singleton dimensions beyond the second are ignored.
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
blanks · char · compose · convertCharsToStrings · convertContainedStringsToChars · convertStringsToChars · genvarname · int2str · isletter · isspace · isStringScalar · isstrprop · mat2str · native2unicode · newline · num2str · sprintf · sscanf · str2double · str2num · strcmp · strcmpi · string · string.empty · strlength · strncmp · strncmpi · strtok · unicode2native
Text Analytics
addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · bagOfWords · cosineSimilarity · doc2sequence · encode · extractFileText · extractHTMLText · fastTextWordEmbedding · findElement · getAttribute · htmlTree · ind2word · isVocabularyWord · normalizeWords · readWordEmbedding · removeLongWords · removeShortWords · removeStopWords · removeWords · stopWords · tokenDetails · tokenizedDocument · trainWordEmbedding · vaderSentimentScores · vec2word · word2ind · word2vec · wordEncoding · writeWordEmbedding
Transform
append · deblank · erase · eraseBetween · erasePunctuation · eraseURLs · extractAfter · extractBefore · extractBetween · insertAfter · insertBefore · join · lower · pad · replace · replaceBetween · reverse · split · splitlines · strcat · strip · strjoin · strjust · strrep · strsplit · strtrim · upper
Search
contains · endsWith · matches · startsWith · strfind
Pattern
digitsPattern · lettersPattern · pattern · regexpPattern · textBoundary · wildcardPattern
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.