What does the strlength function do in MATLAB / RunMat?
strlength(str) counts how many characters appear in each element of text inputs. It works with string
arrays, character vectors, character arrays, and cell arrays of character vectors, returning a double
array that mirrors the input shape.
How does the strlength function behave in MATLAB / RunMat?
- 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.
strlength Function GPU Execution Behaviour
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 of using the strlength function in MATLAB / RunMat
Measure Characters In A String Scalar
len = strlength("RunMat");
Expected output:
len = 6
Count Characters Across A String Array
labels = ["North" "South" "East" "West"];
counts = strlength(labels);
Expected output:
counts = 1×4
5 5 4 4
Compute Lengths For Each Row Of A Character Array
names = char("cat", "giraffe");
row_counts = strlength(names);
Expected output:
row_counts = 2×1
3
7
Handle Empty And Blank Strings
mixed = ["", " "];
len = strlength(mixed);
Expected output:
len = 1×2
0 3
Get Lengths From A Cell Array Of Character Vectors
C = {'red', 'green', 'blue'};
L = strlength(C);
Expected output:
L = 1×3
3 5 4
Treat 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 5
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.
See Also
string, char, strtrim, length, size
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/strings/core/strlength.rs - Found an issue? Please open a GitHub issue with a minimal reproduction.