简体   繁体   中英

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. 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 . Then you can re-call the inputdlg after the error dialog.

To keep the errordlg box from being covered up, use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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