简体   繁体   English

使用inputdlg处理错误

[英]Error handling with inputdlg

Below is an input dialog box I'm using in a program. 下面是我在程序中使用的输入对话框。 Does anyone know how to "nicely" handle the case when the user input is not a number? 没有人知道当用户输入的数字不是数字时如何“巧妙地”处理这种情况吗? Also, if the number is outside the range minlev - maxlev then the error dialog pops up, but you cannot press the OK button because the input dialog pops up in front of it. 另外,如果数字在minlev-maxlev范围之外,则会弹出错误对话框,但是您不能按OK按钮,因为输入对话框会弹出在其前面。 Does anyone know how to fix this? 有谁知道如何解决这一问题?

RVP= 1;


while ( RVP )

prompt = {'Enter the corridor width (1050-1400mm) :'};

dlg_title = 'Input';

num_lines=1;

answer = inputdlg(prompt,dlg_title,num_lines);

    if(str2num(answer{1})<1050 || (str2num(answer{1})>1400))
       errordlg('Number is out of range');

    else 
        w1 = (2*answer{1}-1050-1400)/(1400-1050)


    end
end

Use isnumeric . 使用isnumeric Then you can re-call the inputdlg after the error dialog. 然后,您可以在错误对话框之后重新调用inputdlg。

To keep the errordlg box from being covered up, use uiwait. 为了防止errordlg框被遮盖,请使用uiwait。

while ( RVP )
    prompt = {'Enter the corridor width (1050-1400mm) :'};
    dlg_title = 'Input';
    num_lines=1;
    answer = inputdlg(prompt,dlg_title,num_lines);
    if ~isnumeric(answer) || (str2num(answer{1})<1050 || (str2num(answer{1})>1400))
        uiwait(errordlg('Number is out of range'));
        answer = inputdlg({'Please enter a valid input (1050-1400mm) :'},...
                          dlg_title,num_lines);
    end
        w1 = (2*answer{1}-1050-1400)/(1400-1050)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM