cellstr — Convert text values to a cell array of character vectors in MATLAB and RunMat.
cellstr converts character arrays, string arrays, or cell text values into a cell array of character row vectors. Trailing spaces from character-array rows are trimmed, consistent with MATLAB behavior.
Syntax
C = cellstr(str)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
str | Any | Yes | — | Character array, string array, string scalar, or text cell array. |
Returns
| Name | Type | Description |
|---|---|---|
C | Any | Cell array of character vectors. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:cellstr:InvalidInput | The input value is not a supported text container. | cellstr: input must be text-compatible |
RunMat:cellstr:InvalidContents | Cell elements are not valid character vectors or string scalars. | cellstr: cell array elements must be text scalars |
| — | Internal conversion or allocation failed. | cellstr: internal error |
How cellstr works
cellstr(A)with a character arrayAreturns anm × 1cell array whose entries correspond to the rows ofA, with trailing spaces removed from each row.cellstr(S)with a string arraySreturns a cell array of the same size; each string scalar is converted to a character vector.cellstr(C)with a cell arrayCnormalises the contents so that every element becomes a character vector. String scalars are converted, existing character vectors are preserved, and any other type triggers an error just like MATLAB.- String arrays of any dimensionality preserve their exact shape. Ordering follows MATLAB's column-major semantics so downstream indexing sees the expected elements.
- Empty character arrays result in empty cell arrays (for example,
0 × nchar arrays map to0 × 1cells). Empty strings become""character vectors stored in single-cell outputs. - Missing string scalars are rendered as the literal
'<missing>', matching MATLAB's textual representation. - Inputs that are not text (numeric tensors, logical arrays, structs, GPU tensors, etc.) raise a MATLAB-compatible usage error.
- Integer input is explicitly not applicable: scalar and tensor values from all eight integer classes are rejected before any provider lookup or conversion.
- Cell elements must already be text. Any numeric, logical, or GPU value inside the cell raises an error so that downstream code never sees non-text rows.
Does RunMat run cellstr on the GPU?
cellstr is a host-only builtin. Top-level GPU tensors reject without gather. Recursive cell-container handling gathers a nested GPU tensor before text validation, after which the numeric value rejects; no resident text value is converted. Successful results are always allocated in host memory.
GPU memory and residency
No. cellstr executes on the host and has no resident text representation. Top-level resident tensors reject before provider access. A resident numeric value nested in a cell is gathered by recursive container handling, then rejected because cell elements must be text. The output always resides on the CPU heap.
Examples
Converting a character matrix into a column cell array
A = ['apple '; 'berry '; 'citrus'];
C = cellstr(A)Expected output:
C =
3x1 cell array
{'apple'}
{'berry'}
{'citrus'}Removing trailing spaces automatically
words = ['a '; 'b '; 'c '];
C = cellstr(words)Expected output:
C =
3x1 cell array
{'a'}
{'b'}
{'c'}Converting a string array while preserving shape
S = ["north" "south"; "east" "west"];
C = cellstr(S)Expected output:
C =
2x2 cell array
{'north'} {'south'}
{'east'} {'west'}Normalising a cell array that mixes strings and character vectors
C = {"left", 'right'};
out = cellstr(C)Expected output:
out =
1x2 cell array
{'left'} {'right'}Handling empty character arrays
emptyChars = char.empty(0, 5);
C = cellstr(emptyChars);
size(C)Expected output:
ans =
0 1Converting a single string scalar
single = "RunMat";
C = cellstr(single)Expected output:
C =
1x1 cell array
{'RunMat'}Validating non-text inputs
try
cellstr(42);
catch ME
disp(ME.message)
endExpected output:
cellstr: input must be a character array, string array, or cell array of character vectorsUsing cellstr with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how cellstr changes the result.
Run a small cellstr example, explain the result, then change one input and compare the output.
FAQ
Does cellstr trim whitespace inside the text?⌄
No. Only trailing spaces added by rectangular character arrays are removed. Interior spaces and tabs are preserved exactly as supplied.
What happens when a cell element is not text?⌄
cellstr raises an error: every element must be a character vector or string scalar. This matches MATLAB and prevents silent conversion of unsupported data such as numeric arrays.
Can the output stay on the GPU?⌄
Not yet. The resulting cell array always lives on the host. Future versions may add GPU-backed cell containers, in which case the GPU metadata in this builtin will be updated accordingly without breaking user code.
Are string arrays with missing values supported?⌄
Yes. Missing string scalars (rendered as <missing>) convert to the character vector '<missing>' in the output cell array.
How are empty inputs handled?⌄
Empty character arrays become empty cell arrays (0×1). Empty string arrays return empty cell arrays matching their size. Empty strings become single cells that contain a 1×0 character vector.
Does cellstr copy existing cell arrays?⌄
Yes. The builtin returns a fresh cell array where each element is normalised to a character vector. Elements that are already character vectors are cloned so that downstream code can modify the result without mutating the source cell.
Related Cells functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cellstr is executed, line by line, in Rust.
- View the source for cellstr 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.