strlength — Return double character counts for each text element.
strlength(str) counts characters in strings, character rows, and cell-array text elements while preserving their element shape.
Syntax
L = strlength(str)Inputs
str- Type
Any- Required
- Yes
- Description
- String scalar or array, character array, or cell array of text scalars.
Returns
L- Type
NumericArray- Description
- Double character count for each text element.
Errors
RunMat:strlength:InvalidInput- When
- Input is not a string array, character array, or cell array of text scalars.
- Message
strlength: first argument must be a string array, character array, or cell array of character vectors
RunMat:strlength:InvalidCellElement- When
- A cell-array element is not a character row vector or scalar string.
- Message
strlength: cell array elements must be character vectors or string scalars
RunMat:strlength:InternalError- When
- Internal tensor construction failed while building length results.
- Message
strlength: internal error
RunMat:strlength:TooManyOutputs- When
- More than one output is requested.
- Message
strlength: too many output arguments
Behavior
strlength(str) returns double character counts. String arrays preserve their element shape; a string scalar produces one double scalar. Character matrices produce one count per row in a column vector, while cell arrays of text scalars retain the cell-container shape.
Character matrices formed from unequal rows use trailing spaces as row padding, and those padded spaces are excluded from the row count. Explicit trailing spaces in a single character vector remain part of its count.
A missing string element produces NaN. Empty text produces zero lengths or an empty double array with the corresponding element shape. RunMat counts Unicode scalar values, not UTF-8 bytes; parity of every Unicode grapheme case with MATLAB is not asserted.
Numeric and integer arrays are not interpreted as character codes. Convert to text first when the intended operation is to count formatted characters. Provider-resident numeric inputs reject before provider access.
Placement and effects
The default binding measures host text in native and browser runtimes. No text GPU kernel, provider preparation, or fusion form exists. It may throw for nontext input or malformed cell elements but has no callback, workspace, filesystem, randomness, network, or time effect.
An unused result is evaluated and discarded. More than one requested output is invalid. Distributed-input execution is unsupported, separately from ordinary transferability of host text values.
Examples
Count a string scalar
len = strlength("RunMat")Expected output:
len = 6Count each string-array element
labels = ["North" "South" "East" "West"]; counts = strlength(labels)Expected output:
counts = [5 5 4 4]Count character-array rows
names = char("cat", "giraffe"); row_counts = strlength(names)Expected output:
row_counts = [3; 7]Count empty and blank strings
mixed = ["" " "]; len = strlength(mixed)Expected output:
len = [0 3]Count cell-array text
C = {'red', 'green', 'blue'}; L = strlength(C)Expected output:
L = [3 5 4]Missing string length is NaN
values = string(["alpha" "beta" "gamma"]); values(2) = string(missing); lengths = strlength(values)Expected output:
lengths = [5 NaN 5]Using 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
Which numeric class does strlength return?⌄
Double, including when every count is a whole number.
Why are some trailing spaces ignored?⌄
Padding of shorter rows in a multirow character matrix is excluded. Explicit spaces in a one-row character vector remain counted.
What happens with a missing string?⌄
Its count is NaN. Use ismissing or fillmissing when a default is needed.
Can I pass numeric data?⌄
No. Convert numbers to text first with num2str or string if that is the intended operation.
How does RunMat count Unicode text?⌄
It counts Unicode scalar values rather than UTF-8 bytes. Combining sequences may contain more than one scalar value.
Does this run on a GPU?⌄
No. Text is host data; unsupported resident numeric input rejects before provider access.
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 · string.empty · strings · 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 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 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.