Filter löschen
Filter löschen

'Matrix dimensions must agree' error on string operations

3 Ansichten (letzte 30 Tage)
Harshita K
Harshita K am 10 Jun. 2020
Kommentiert: Harshita K am 10 Jun. 2020
My code looks like:
data is 2x1 array.
data =
{'cont_box_A'}
{'cont_box_B'}
I'm trying to compare the string to another string, like:
if(data{1} == 'Test')
fprintf('Not the same')
else
fprintf('Same')
end
When I try to execute this, I get an error saying 'Matrix dimensions must agree'. don't really know what's wrong.

Akzeptierte Antwort

Stephen23
Stephen23 am 10 Jun. 2020
"...don't really know what's wrong."
What you wrote performs an element-wise comparison of the character in two character arrays. Just like for numeric arrays, that operation requires that the arrays have compatible sizes (exactly as described in the documentation, so I won't copy it here).
But what you are trying to do is to compare the entire strings, for which you should use strcmp or strcmpi:
strcmp(data{1},'Test')

Weitere Antworten (1)

madhan ravi
madhan ravi am 10 Jun. 2020
ismember(data{1}, 'Test')

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by