2016-10-11 67 views
0

这里的时候是我的代码:尺寸不匹配的错误比较字符串“==”

a='abc'; 

key = input('Enter secret: ', 's'); 

if key =='abc' 
fprintf('Correct') 
else 
fprintf('Wrong. Re-enter.'); 
end 

当我运行代码,我进入abcdefg,它显示了一个错误:

Error using ==, matrix dimension must be agree.

怎么办我防止这样的错误?

+7

使用[strcmp](https://de.mathworks.com/help/matlab/ref/strcmp.html)来比较字符串。 –

+0

谢谢先生。我忘了这个功能! – kenreal

回答

1

==当矩阵有不同的尺寸时不起作用。 'abc'实际上是一个1x3字符数组。比较字符串时,应使用strcmpstrcmpi。另一种方法是isequal,但我建议使用其他两种之一。