{
"title": "strlength",
"category": "strings/core",
"keywords": [
"strlength",
"string length",
"character count",
"text analytics",
"cell array"
],
"summary": "Return the number of characters in each element of a string array, character array, or cell array of character vectors.",
"references": [
"https://www.mathworks.com/help/matlab/ref/strlength.html"
],
"gpu_support": {
"elementwise": false,
"reduction": false,
"precisions": [],
"broadcasting": "none",
"notes": "Executes on the CPU; if any argument lives on the GPU, the runtime gathers it before computing lengths to keep semantics identical to MATLAB."
},
"fusion": {
"elementwise": false,
"reduction": false,
"max_inputs": 1,
"constants": "inline"
},
"requires_feature": null,
"tested": {
"unit": "builtins::strings::core::strlength::tests",
"integration": "builtins::strings::core::strlength::tests::strlength_cell_array_of_char_vectors"
},
"description": "`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.",
"behaviors": [
"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)`) yield `NaN`. 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."
],
"examples": [
{
"description": "Measure Characters In A String Scalar",
"input": "len = strlength(\"RunMat\")",
"output": "len = 6"
},
{
"description": "Count Characters Across A String Array",
"input": "labels = [\"North\" \"South\" \"East\" \"West\"];\ncounts = strlength(labels)",
"output": "counts = 1×4\n 5 5 4 4"
},
{
"description": "Compute Lengths For Each Row Of A Character Array",
"input": "names = char(\"cat\", \"giraffe\");\nrow_counts = strlength(names)",
"output": "row_counts = 2×1\n 3\n 7"
},
{
"description": "Handle Empty And Blank Strings",
"input": "mixed = [\"\", \" \"];\nlen = strlength(mixed)",
"output": "len = 1×2\n 0 3"
},
{
"description": "Get Lengths From A Cell Array Of Character Vectors",
"input": "C = {'red', 'green', 'blue'};\nL = strlength(C)",
"output": "L = 1×3\n 3 5 4"
},
{
"description": "Treat Missing Strings As NaN",
"input": "values = string([\"alpha\" \"beta\" \"gamma\"]);\nvalues(2) = string(missing); % Displays as <missing> when printed\nlengths = strlength(values)",
"output": "lengths = 1×3\n 5 NaN 5"
}
],
"faqs": [
{
"question": "What numeric type does `strlength` return?",
"answer": "`strlength` always returns doubles, even when all lengths are whole numbers. MATLAB uses doubles for most numeric results, and RunMat follows the same rule."
},
{
"question": "Why are padded spaces in character arrays ignored?",
"answer": "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."
},
{
"question": "How are missing string values handled?",
"answer": "Missing string scalars display as `<missing>` and produce `NaN` lengths. Use `ismissing` or `fillmissing` if you need to substitute a default length."
},
{
"question": "Can I call `strlength` with numeric data?",
"answer": "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."
},
{
"question": "Does `strlength` support multibyte Unicode characters?",
"answer": "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."
},
{
"question": "Will `strlength` ever execute on the GPU?",
"answer": "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."
}
],
"links": [
{
"label": "string",
"url": "./string"
},
{
"label": "char",
"url": "./char"
},
{
"label": "strtrim",
"url": "./strtrim"
},
{
"label": "length",
"url": "./length"
},
{
"label": "size",
"url": "./size"
},
{
"label": "compose",
"url": "./compose"
},
{
"label": "num2str",
"url": "./num2str"
},
{
"label": "sprintf",
"url": "./sprintf"
},
{
"label": "str2double",
"url": "./str2double"
},
{
"label": "strcmp",
"url": "./strcmp"
},
{
"label": "strcmpi",
"url": "./strcmpi"
},
{
"label": "string.empty",
"url": "./string.empty"
},
{
"label": "strings",
"url": "./strings"
},
{
"label": "strncmp",
"url": "./strncmp"
}
],
"source": {
"label": "`crates/runmat-runtime/src/builtins/strings/core/strlength.rs`",
"url": "https://github.com/runmat-org/runmat/blob/main/crates/runmat-runtime/src/builtins/strings/core/strlength.rs"
},
"gpu_behavior": [
"`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."
]
}