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
X- Type
Any- Required
- Yes
- Description
- Input value to convert to string array.
encoding- Type
StringScalar- Required
- No
- Default
"UTF-8"- Description
- Character encoding (UTF-8 aliases supported).
formatSpec- Type
Any- Required
- Yes
- Description
- Format specification text/cell/string array.
A- Type
Any- Required
- Variadic
- Description
- Formatting data arguments.
Returns
S- Type
Any- Description
- String scalar/array result.
Errors
RunMat:string:InvalidInput- When
- Input conversion/formatting/encoding constraints are violated.
- Message
string: invalid input
Behavior
Scalar inputs return a 1×1 string 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 and string(X, encoding) as mode-gated extensions. They are available in runmat mode but are not presented as documented string(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.
Execution and placement
Conversion returns host strings. Automatic resident data gathers through its owning provider; explicit GPU numeric input and formatting/encoding forms are mode-gated extensions. Conversion of user-defined objects remains unsupported.
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.14159" "2.71828" "1.41421"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?⌄
RunMat uses its shared numeric-to-text conversion. Finite floating values use their standard decimal representation; NaN and infinities are rendered as NaN and Inf. This is not a claim of exact MATLAB display-format parity.
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. RunMat renders complex values using the i suffix, such as 1+2i or 1-2i; this does not assert exact parity with MATLAB's numeric display formatting.
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
Source you can read
Every RunMat function's source is public. 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 a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.