strcmp — Compare text inputs for exact case-sensitive equality with MATLAB-compatible implicit expansion.
strcmp(a, b) returns logical true when corresponding text values match exactly and false otherwise. It supports MATLAB-compatible string, char, and cellstr-style inputs with implicit expansion.
Syntax
tf = strcmp(A, B)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | First text input (string/char/cell/string array). |
B | Any | Yes | — | Second text input (string/char/cell/string array). |
Returns
| Name | Type | Description |
|---|---|---|
tf | LogicalArray | Logical comparison result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:strcmp:InvalidInput | At least one input is not a supported text container. | strcmp: text inputs must be string/char/cell/string-array values |
RunMat:strcmp:ShapeMismatch | Inputs are not broadcast-compatible for elementwise comparison. | strcmp: input sizes are not broadcast-compatible |
RunMat:strcmp:InternalError | Internal logical result assembly failed. | strcmp: internal error |
How strcmp works
- Accepted text types: Works with string scalars/arrays, character vectors or matrices created with
char, and cell arrays of character vectors. Mixed combinations are converted automatically, matching MATLAB. - Implicit expansion: Scalar inputs expand to the shape of the other operand, producing element-wise comparisons for higher-dimensional arrays.
- Character arrays: Rows are compared individually. The result is a column vector whose length equals the number of rows in the character array.
- Cell arrays: Each cell is treated as a text scalar. Scalar cell arrays expand across the other operand before comparison.
- Missing strings: String elements equal to
missingcompare unequal to everything, including othermissingvalues. - Result form: A single logical scalar is returned for scalar comparisons; otherwise you receive a logical array using column-major MATLAB layout.
- Case sensitivity: Matching is case-sensitive. Use
strcmpifor case-insensitive comparisons. - Unsupported datatypes: Following the documented text-comparison rule, integer and other unsupported numeric inputs return scalar logical
false; RunMat does not gather or stringify them.
Does RunMat run strcmp on the GPU?
strcmp is a host text comparison sink. Numeric resident operands are unsupported datatypes and return scalar logical false without gather or backend-specific kernels.
GPU memory and residency
strcmp compares host text and returns host logical output. Unsupported numeric resident operands return false without provider access.
Examples
Compare Two Equal Strings
tf = strcmp("RunMat", "RunMat")Expected output:
tf = logical
1Compare String Array With Scalar Text
names = ["red" "green" "blue"];
tf = strcmp(names, "green")Expected output:
tf = 1×3 logical array
0 1 0Compare Character Array Rows
labels = char("cat", "dog", "cat");
tf = strcmp(labels, "cat")Expected output:
tf = 3×1 logical array
1
0
1Compare Two Cell Arrays Of Character Vectors
C1 = {'apple', 'pear', 'grape'};
C2 = {'apple', 'peach', 'grape'};
tf = strcmp(C1, C2)Expected output:
tf = 1×3 logical array
1 0 1Handle Missing Strings
vals = ["alpha" missing];
tf = strcmp(vals, "alpha")Expected output:
tf = 1×2 logical array
1 0Implicit Expansion With Column Vector Text
patterns = char("north", "south");
tf = strcmp(patterns, ["north" "east"])Expected output:
tf = 2×2 logical array
1 0
0 0Using strcmp with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how strcmp changes the result.
Run a small strcmp example, explain the result, then change one input and compare the output.
FAQ
What types can I pass to strcmp?⌄
Use string arrays, character vectors/arrays, or cell arrays of character vectors. Mixed combinations are accepted and follow MATLAB's implicit expansion rules.
Does strcmp ignore letter case?⌄
No. strcmp is case-sensitive. Use strcmpi for case-insensitive comparisons.
What happens when the inputs contain missing strings?⌄
Missing string scalars compare unequal to every value (including other missing strings), so the result is false.
Can strcmp compare matrices of characters?⌄
Yes. Character arrays compare row-by-row, returning a column vector whose entries tell you whether each row matches.
Does strcmp return numeric or logical results?⌄
It returns logical results. Scalars become logical scalars (Value::Bool), while arrays are returned as logical arrays.
Related Strings functions
Core
blanks · char · compose · convertCharsToStrings · convertContainedStringsToChars · convertStringsToChars · genvarname · int2str · isletter · isspace · isStringScalar · isstrprop · mat2str · native2unicode · newline · num2str · sprintf · sscanf · str2double · str2num · strcmpi · string · string.empty · strings · strlength · 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
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strcmp is executed, line by line, in Rust.
- View the source for strcmp 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.