If statement comparing strings

59 visualizaciones (últimos 30 días)
Sam Falzone
Sam Falzone el 5 de Abr. de 2011
Comentada: Walter Roberson el 28 de Nov. de 2017
I want to do this:
if 'Word1' == 'Word2' 'do something' end
'Word1' and 'Word2' aren't necessarily going to be the same amount of characters.
Everything I try ends in "eq error".
How do I do this?

Respuestas (1)

Walter Roberson
Walter Roberson el 5 de Abr. de 2011
Editada: Walter Roberson el 28 de Nov. de 2017
strcmp('Word1', 'Word2')
OR
all(size('Word1') == size('Word2')) && all('Word1' == 'Word2')
OR
isequal('Word1', 'Word2')
  2 comentarios
Zhuoying Lin
Zhuoying Lin el 28 de Nov. de 2017
Editada: Walter Roberson el 28 de Nov. de 2017
Hi I have a similar question. I type:
if isequal(x,'a') && isequal(x,'p') && isequal(x,'T')==0
fprintf('ERROR:You entered incorrect choice.')
but it doesn't work
Walter Roberson
Walter Roberson el 28 de Nov. de 2017
if ~ismember(x, {'a', 'p', 'T'))
printf('ERROR:You entered incorrect choice.');
end
or
if ~(isequal(x, 'a') || isequal(x, 'p') || isequal(x, 'T'))
fprintf('ERROR:You entered incorrect choice.');
end
or
if ~(strcmp(x, 'a') || strcmp(x, 'p') || strcmp(x, 'T'))
fprintf('ERROR:You entered incorrect choice.');
end
or
if ~strcmp(x, 'a') && ~strcmp(x, 'p') && ~strcmp(x, 'T')
fprintf('ERROR:You entered incorrect choice.');
end
or
if ~any([strcmp(x, 'a'), strcmp(x, 'p'), strcmp(x, 'T')])
fprintf('ERROR:You entered incorrect choice.');
end

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by