Borrar filtros
Borrar filtros

'Matrix dimensions must agree' error on string operations

4 visualizaciones (últimos 30 días)
Harshita K
Harshita K el 10 de Jun. de 2020
Comentada: Harshita K el 10 de Jun. de 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.

Respuesta aceptada

Stephen23
Stephen23 el 10 de Jun. de 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')

Más respuestas (1)

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

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by