2

如何避免用户直接关闭输入对话框而不输入任何值?对于“菜单”功能,我们可以使用 while options==0 来形成循环,用于未选择的选项,但是输入对话框呢?

prompt = {'输入增益:','输入范围:'};

dlg_title = '输入值';

num_lines = 1;

def = {'20','256'}; %默认

答案 = inputdlg(提示,dlg_title,num_lines,def);

%%%获取输入的两个值%%%%

%A = getfield(answer,{1}); %第一个输入字段

A = str2double(answer{1});

%B = getfield(answer,{2}); %第二个输入字段

B = str2double(answer{2});

假设我有一个如图所示的输入对话框,我如何用循环完整地建模它

4

1 回答 1

1

您无法阻止它被关闭,但您可以使用 while 循环重新打开它,直到用户输入有用的值。

done = false;
while ~done
    a=inputdlg('enter a number')
    num = str2double(a{1}); %# will turn empty and strings into NaN
    if isnumeric(num)
       done = true;
    else
       %# keep running while loop
       %# you can pop up an errordlg box here to tell the user what was wrong.
       %# It would be nice if you were to set the inputs that passed the test
       %# as defaults for the next call of inputdlg. Nothing sucks as much 
       %# as having to completely re-fill a form because of a small typo
    end
end
于 2012-12-29T04:00:46.540 回答