Reputation: 35
my system will give me the a/m error when I input more than 3 characters
a = input('Please type f0 or f1: ' , 's');
if a == 'f0';
Run_f0
elseif a == 'f1';
Run_f1
else
disp('Please enter f0 or f1 only');
end
what should I do to resolve this error? Thanks in advance
Upvotes: 0
Views: 1264
Reputation: 4311
Matlab will compare each character of both strings. If one string is longer than the other one, there is nothing to compare and it will throw an error. You can bypass this by forcing the user to repeat the input until he gives a valid input:
valid = {'f0', 'f1'}
a = input('Please type f0 or f1: ' , 's');
while not(ismember(a, valid)) %// or: while not(any(strcmp(a, valid)))
a = input('Please really type f0 or f1: ' , 's');
end
The user will be asked to really input 'f0' or 'f1'.
As an alternative, you can consider to compare the strings with strcmp()
:
if strcmp(a, 'f0')
%// something
elseif strmpc(a, 'f1')
%// something else
else
disp('Please enter f0 or f1 only');
end
Upvotes: 5
Reputation: 2385
To compare strings you should use the function strcmp
a='12345'
strcmp('f01',a)
Returns: 0 (False)
a='f01'
strcmp('f01',a)
Returns: 1 (True)
Upvotes: 1