strlength — Return per-element character counts for text inputs with MATLAB-compatible output shaping.
strlength(str) counts characters in each text element across string, char, and cellstr-style inputs. It returns MATLAB-compatible double arrays shaped to match elementwise input structure.
Syntax
L = strlength(str)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
str | Any | Yes | — | String array, character array, or cell array of text scalars. |
Returns
| Name | Type | Description |
|---|---|---|
L | NumericArray | Character counts for each text element. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:strlength:InvalidInput | Input is not a string array, character array, or cell array of text scalars. | strlength: first argument must be a string array, character array, or cell array of character vectors |
RunMat:strlength:InvalidCellElement | A cell-array element is not a character row vector or scalar string. | strlength: cell array elements must be character vectors or string scalars |
RunMat:strlength:InternalError | Internal tensor construction failed while building length results. | strlength: internal error |
How strlength works
- String arrays return a numeric array of the same size; string scalars yield a scalar
double. - Character arrays report the number of characters per row and ignore padding that MATLAB inserts to keep rows the same width.
- Character vectors stored in cells contribute one scalar per cell element; the output array matches the cell array shape.
- Missing string scalars (for example values created with
string(missing)) yieldNaN. RunMat displays these entries as<missing>in the console just like MATLAB. - Empty text inputs produce zeros-sized numeric outputs that match MATLAB's dimension rules.
Does RunMat run strlength on the GPU?
strlength is a metadata query and always executes on the CPU. If a text container references data that originated on the GPU (for example, a cell array that still wraps GPU-resident numeric intermediates), RunMat gathers that data before measuring lengths. Providers do not require custom kernels for this builtin.
Examples
Measure Characters In A String Scalar
len = strlength("RunMat")Expected output:
len = 6Count Characters Across A String Array
labels = ["North" "South" "East" "West"];
counts = strlength(labels)Expected output:
counts = 1×4
5 5 4 4Compute Lengths For Each Row Of A Character Array
names = char("cat", "giraffe");
row_counts = strlength(names)Expected output:
row_counts = 2×1
3
7Handle Empty And Blank Strings
mixed = ["", " "];
len = strlength(mixed)Expected output:
len = 1×2
0 3Get Lengths From A Cell Array Of Character Vectors
C = {'red', 'green', 'blue'};
L = strlength(C)Expected output:
L = 1×3
3 5 4Treat Missing Strings As NaN
values = string(["alpha" "beta" "gamma"]);
values(2) = string(missing); % Displays as <missing> when printed
lengths = strlength(values)Expected output:
lengths = 1×3
5 NaN 5Using strlength with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how strlength changes the result.
Run a small strlength example, explain the result, then change one input and compare the output.
FAQ
What numeric type does strlength return?⌄
strlength always returns doubles, even when all lengths are whole numbers. MATLAB uses doubles for most numeric results, and RunMat follows the same rule.
Why are padded spaces in character arrays ignored?⌄
When MATLAB builds a character array from rows of different lengths, it pads shorter rows with spaces. Those padding characters are not part of the logical content, so strlength removes them before counting. Explicit trailing spaces that you type in a single character vector remain part of the count.
How are missing string values handled?⌄
Missing string scalars display as <missing> and produce NaN lengths. Use ismissing or fillmissing if you need to substitute a default length.
Can I call strlength with numeric data?⌄
No. strlength only accepts string arrays, character vectors/arrays, or cell arrays of character vectors. Numeric inputs raise an error—use num2str first if you need to convert numbers to text.
Does strlength support multibyte Unicode characters?⌄
Yes. Each Unicode scalar value counts as one character, so emoji or accented letters contribute a length of one. Surrogate pairs are treated as a single character, matching MATLAB's behaviour.
Will strlength ever execute on the GPU?⌄
No. The builtin inspects metadata and operates on host strings. If your data already lives on the GPU, RunMat gathers it automatically before computing lengths so results match MATLAB exactly.
Related Strings functions
Core
char · compose · num2str · sprintf · str2double · strcmp · strcmpi · string · string.empty · strings · strncmp
Search
contains · endsWith · startsWith · strfind
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strlength is executed, line by line, in Rust.
- View the source for strlength 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.