'Matrix dimensions must agree' error on string operations
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuesta aceptada
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)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!