Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MatLab:整数输入的错误处理_Matlab_Error Handling_Try Catch_User Input_Validation - Fatal编程技术网

MatLab:整数输入的错误处理

MatLab:整数输入的错误处理,matlab,error-handling,try-catch,user-input,validation,Matlab,Error Handling,Try Catch,User Input,Validation,在MatLab中,当用户在输入中输入字母和其他非数字的东西时,我如何捕捉occours所产生的错误: width = input('Enter a width: '); 我已经用try/catch命令玩了一段时间: width = 0; message = ''; % Prompting. while strcmp(message,'Invalid.') || width < 1 || width ~= int32(width) try disp(message)

在MatLab中,当用户在输入中输入字母和其他非数字的东西时,我如何捕捉occours所产生的错误:

width = input('Enter a width: ');
我已经用
try/catch
命令玩了一段时间:

width = 0;
message = '';

% Prompting.
while strcmp(message,'Invalid.') || width < 1 || width ~= int32(width)

  try
     disp(message)
     width = input('Frame width: ');
  catch error
     message = 'Invalid.';
  end

end
width=0;
信息=“”;
%提示。
而strcmp(消息“无效”)| |宽度<1 | |宽度~=int32(宽度)
尝试
显示(信息)
宽度=输入('帧宽度:');
捕捉错误
消息='无效';
终止
终止
但是没有运气(上面的方法不起作用)。如图所示,我想要一条简单的消息,如“Frame width:”供用户第一次输入他的选择。但是,如果发现错误,我希望他收到的信息是“无效。再试一次:”每次出现错误时fx

我还尝试了
错误()
,但我不知道如何正确放置。由于
error()
没有将发生错误的
input
命令作为参数,因此它必须以另一种方式检测错误,我无法理解

任何帮助都将不胜感激

width = input('Frame width: ');
while(~isInt(width))
    width = input('Invalid. Try again: ');
end
您必须在某个地方(或它的另一个实现)具有以下函数

您必须在某个地方(或它的另一个实现)具有以下函数

status
如果转换失败,则为0。如果没有
isscalar
测试,也将接受[1 2;3 4]之类的输入。最后一个测试确保宽度必须为整数。)



status
如果转换失败,则为0。如果没有
isscalar
测试,也将接受类似[1 2;3 4]的输入。最后一次测试确保宽度必须为整数。)

+1。我要指出的是,你可以在那里扔一个
isscalar
,而不是
if/else
语句。当我输入一个字母时,它不起作用。。。错误发生在
width=input('Frame width:')的行上。似乎当输入字母(或任何字符串)时,此处会发生错误,因为
input()
没有要求输入字符串。然后程序在
循环时永远不会到达
。相反,它会在这一行中断。@即使您输入一个周围有
'
的字符串,会发生什么?如果不将字符串括起来,它会认为您试图从工作区输入变量并计算该变量。如果发生错误,它应该只是再次提示,而不是中断整个。因此,如果要避免
input()
参数,我必须在输入字符串周围添加撇号?这对用户来说并不友好。那么,是否不可能捕捉到由于发送字符串而导致occours的错误?字符串只使用撇号,就像在普通代码中一样:
4
是整数,
4.5
是双精度,
a
是变量,
'b'
是字符串。您可以始终使用
's'
参数定义您的输入始终是字符串。。但既然你在逃避,我想我们被困住了。我要指出的是,你可以在那里扔一个
isscalar
,而不是
if/else
语句。当我输入一个字母时,它不起作用。。。错误发生在
width=input('Frame width:')的行上。似乎当输入字母(或任何字符串)时,此处会发生错误,因为
input()
没有要求输入字符串。然后程序在
循环时永远不会到达
。相反,它会在这一行中断。@即使您输入一个周围有
'
的字符串,会发生什么?如果不将字符串括起来,它会认为您试图从工作区输入变量并计算该变量。如果发生错误,它应该只是再次提示,而不是中断整个。因此,如果要避免
input()
参数,我必须在输入字符串周围添加撇号?这对用户来说并不友好。那么,是否不可能捕捉到由于发送字符串而导致occours的错误?字符串只使用撇号,就像在普通代码中一样:
4
是整数,
4.5
是双精度,
a
是变量,
'b'
是字符串。您可以始终使用
's'
参数定义您的输入始终是字符串。。但是,既然您要避免这样做,我想我们就卡住了。您可能还想确保不允许使用小数部分(标题表明需要整数输入)。不幸的是,我需要避免在
input()
命令中使用
's'
参数。我正在寻找一个解决方案,在不使用此参数时捕获错误。有趣。为什么要避免“s”?需要将输入作为字符串处理。通常的做法是以字符串形式接收用户输入,然后尝试转换它,并在第二步检查转换结果。我不得不放弃它。我确信。当无法避免使用
's'
时,您的回答起了作用。您可能还希望确保不允许使用小数部分(标题表明需要整数输入)。不幸的是,我需要避免在
input()
命令中使用
's'
参数。我正在寻找一个解决方案,在不使用此参数时捕获错误。有趣。为什么要避免“s”?需要将输入作为字符串处理。通常的做法是以字符串形式接收用户输入,然后尝试转换它,并在第二步检查转换结果。我不得不放弃它。我确信。当
无法避免时,您的回答起了作用。
function retval = isInt(val)
    retval = isscalar(val) && isnumeric(val) && isreal(val) && isfinite(val) && (val == fix(val));
end
answer = input('Frame width: ', 's');
[width, status] = str2num(answer);
while ~status || ~isscalar(width) || width ~= floor(width)
  answer = input('Invalid. Try again: ', 's');
  [width, status] = str2num(answer);
end
disp(width);