string — Convert numeric, logical, character, and text inputs into MATLAB-compatible string arrays.
string(A) converts numeric, logical, character, cell, or existing string data into a string array. Integer values are formatted directly from native storage, so wide int64 and uint64 values do not pass through double or lose digits.
Syntax
S = string(X)
S = string(X, encoding)
S = string(formatSpec, A...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Input value to convert to string array. |
encoding | StringScalar | No | "UTF-8" | Character encoding (UTF-8 aliases supported). |
formatSpec | Any | Yes | — | Format specification text/cell/string array. |
A | Any | Variadic | — | Formatting data arguments. |
Returns
| Name | Type | Description |
|---|---|---|
S | Any | String scalar/array result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:string:InvalidInput | Input conversion/formatting/encoding constraints are violated. | string: invalid input |
How string works
- Scalar inputs return a
1×1string array containing the converted value. - Numeric and logical arrays preserve the original shape while converting each element; all eight integer classes retain exact decimal values, including values wider than binary64 can represent.
- Character arrays turn into columnar string arrays with one row per original row.
- Cell arrays must contain text-like or scalar numeric values; each cell becomes one string scalar.
- RunMat also provides
string(formatSpec, A1, ..., An)printf-style formatting andstring(X, encoding)as mode-gated extensions. They are available inrunmatmode but are not presented as documentedstring(A)forms. - Empty inputs yield empty string arrays that match MATLAB's dimension rules.
- Unsupported types (structs, handle objects, events, functions) raise descriptive errors. Explicit GPU numeric input is separately gated, while an automatically resident intermediate may gather at this host string sink.
Does RunMat run string on the GPU?
string is a residency sink. Automatically resident values gather through their owning provider before host conversion. Explicit gpuArray input is registered and enforced as a separate RunMat-only extension, so compatibility mode does not silently accept undocumented device intent.
Examples
Converting A Numeric Scalar To A String
name = string(42)Expected output:
name = "42"Turning A Numeric Row Vector Into Strings
values = string([3.14159 2.71828 1.41421])Expected output:
values = 1×3 string
"3.1416" "2.7183" "1.4142"Converting A Character Matrix Into A String Array
C = ['North '; 'South '; 'East '; 'West '];
regions = string(C)Expected output:
regions = 4×1 string
"North "
"South "
"East "
"West "Converting Logical Data To String Scalars
flags = string(logical([1 0 1 0]))Expected output:
flags = 1×4 string
"true" "false" "true" "false"Creating Strings From A Cell Array Of Mixed Scalars
C = {true, 17, "runmat"};
S = string(C)Expected output:
S = 1×3 string
"true" "17" "runmat"Formatting Numeric Values
labels = string("Trial %d", 1:4)Expected output:
labels = 1×4 string
"Trial 1" "Trial 2" "Trial 3" "Trial 4"Converting GPU-resident numeric data to strings
G = gpuArray([10 20 30]);
labels = string(G)Expected output:
labels = 1×3 string
"10" "20" "30"Using string with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how string changes the result.
Run a small string example, explain the result, then change one input and compare the output.
FAQ
Does string change the size of my array?⌄
No. Array-shaped inputs return string arrays with the same shape. Character arrays become column vectors where each row of characters maps to one string scalar.
How are floating-point numbers formatted?⌄
Floating-point values use MATLAB's short-g formatting (up to 12 significant digits) so the result matches disp output and is consistent across CPU and GPU inputs.
Can I use format specifiers like %0.2f?⌄
In runmat mode, yes. RunMat provides the mode-gated extension string(formatSpec, A1, ..., An); for example, string("Value %0.2f", A). MATLAB-compatible mode accepts the documented string(A) conversion forms instead.
What happens if I pass a GPU tensor?⌄
An automatically resident intermediate is downloaded through the provider that owns it and converted on the CPU. Explicit gpuArray input is a RunMat extension and is rejected in MATLAB-compatible mode. The result is always host-resident text.
Can I request a specific character encoding?⌄
In runmat mode, the mode-gated encoding extension accepts 'UTF-8', 'utf8', or 'system'; each selects RunMat's UTF-8 conversion. MATLAB-compatible mode does not expose this extra string(X, encoding) form.
Can I convert complex numbers or complex arrays?⌄
Yes. Complex scalars and arrays use MATLAB's a + bi formatting, with imaginary values rendered using the i suffix.
What happens with empty inputs?⌄
Empty inputs return empty string arrays following MATLAB's dimension rules—for example, string([]) yields a 0×0 string array, and string(char.empty(0,5)) yields a 0×1 string array.
Why does string error on structs or handle objects?⌄
RunMat converts supported text, logical, and numeric scalars or arrays element by element. Structs, unsupported objects, listeners, and function values do not have an implicit text conversion and therefore raise an error.
How can I keep trailing spaces from character arrays?⌄
string preserves every character, including trailing spaces. Use strtrim afterwards if you want to remove padding.
Do existing string arrays change when passed to string?⌄
No. Existing string arrays pass through unchanged, so string(["a" "b"]) returns the same array.
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.empty · strings · 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 string is executed, line by line, in Rust.
- View the source for string 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.