本文介绍了使用==时出错矩阵尺寸必须一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入3个以上的字符时,系统会提示我a/m错误

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

推荐答案

Matlab将比较两个字符串的每个字符.如果一个字符串比另一个字符串长,则没有什么可比较的,它将引发错误.您可以通过强制用户重复输入,直到用户提供有效的输入来绕过此操作:

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

将要求用户真正输入"f0"或"f1".

The user will be asked to really input 'f0' or 'f1'.

作为替代方案,您可以考虑将字符串与strcmp()进行比较:

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

这篇关于使用==时出错矩阵尺寸必须一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:38