matlab 指南中用于检查无效值的函数
Function in matlab guide to check for invalid values
我正在为我的 Matlab 指南程序编写一个函数。我想在指南中应用 3 个文本框的限制,从 0 到 1,它应该只是数字。 (如果用户输入的值无效,则会生成一个错误框)。问题是我想为此编写一个函数,而不是在每个文本框的回调中编写限制代码。用户也没有必要一次输入所有值,而是当用户输入三个值中的任何一个并生成反馈时,函数应该 运行。我写的功能如下,但它不工作。 (没有必要将所有三个输入都提供给函数,这就是我在输入之间使用 || 的原因)
function CheckMe(maxMBT || minMBT || mainMBT)
max_MBT= str2double(get(hObject, 'String'));
if isnan(maxMBT)||maxMBT < 0|| maxMBT> 1
errordlg('Invalid max value for MBT. Please enter values between 0 to 1');
set(max_MBT, 'String', 0);
if isnan(minMBT)||minMBT < 0|| minMBT> 1
set(min_MBT, 'String', 0);
errordlg('Invalid min value for MBT. Please enter values between 0 to 1');
if isnan(mainMBT)||mainMBT < 0 || mainMBT >1
set(edtMBT, 'String', 0);
errordlg('Invalid value of MBT. Enter values between 0 to 1');
end
end
end
您的语法错误,可选参数未通过 ||
分隔传递。相反,我建议使用 2 个输入:
- 输入您要检查的值
- 根据触发的回调输入 'type' 的值。
该函数看起来像这样:
function valid = CheckMe( userInput, boxType )
% This checks for valid inputs between 0 and 1.
% USERINPUT should be a string from the input text box
% BOXTYPE should be a string specified by the callback, to identify the box
% Do the check on the userInput value
userInput = str2double( userInput );
if isnan( userInput ) || userInput < 0 || userInput > 1
% boxType specific error message
errordlg(['Invalid value for ' boxType '. Please enter values between 0 to 1']);
% Output flag
valid = false;
else
valid = true;
end
end
此函数 returns 一个布尔变量 valid
,您可以像这样在回调函数中使用它:
validatedInput = CheckMe( '0.5', 'TestBox' ); % Using the function to check input
if ~validatedInput
% Input wasn't valid!
myTextBox.String = '0';
end
我正在为我的 Matlab 指南程序编写一个函数。我想在指南中应用 3 个文本框的限制,从 0 到 1,它应该只是数字。 (如果用户输入的值无效,则会生成一个错误框)。问题是我想为此编写一个函数,而不是在每个文本框的回调中编写限制代码。用户也没有必要一次输入所有值,而是当用户输入三个值中的任何一个并生成反馈时,函数应该 运行。我写的功能如下,但它不工作。 (没有必要将所有三个输入都提供给函数,这就是我在输入之间使用 || 的原因)
function CheckMe(maxMBT || minMBT || mainMBT)
max_MBT= str2double(get(hObject, 'String'));
if isnan(maxMBT)||maxMBT < 0|| maxMBT> 1
errordlg('Invalid max value for MBT. Please enter values between 0 to 1');
set(max_MBT, 'String', 0);
if isnan(minMBT)||minMBT < 0|| minMBT> 1
set(min_MBT, 'String', 0);
errordlg('Invalid min value for MBT. Please enter values between 0 to 1');
if isnan(mainMBT)||mainMBT < 0 || mainMBT >1
set(edtMBT, 'String', 0);
errordlg('Invalid value of MBT. Enter values between 0 to 1');
end
end
end
您的语法错误,可选参数未通过 ||
分隔传递。相反,我建议使用 2 个输入:
- 输入您要检查的值
- 根据触发的回调输入 'type' 的值。
该函数看起来像这样:
function valid = CheckMe( userInput, boxType )
% This checks for valid inputs between 0 and 1.
% USERINPUT should be a string from the input text box
% BOXTYPE should be a string specified by the callback, to identify the box
% Do the check on the userInput value
userInput = str2double( userInput );
if isnan( userInput ) || userInput < 0 || userInput > 1
% boxType specific error message
errordlg(['Invalid value for ' boxType '. Please enter values between 0 to 1']);
% Output flag
valid = false;
else
valid = true;
end
end
此函数 returns 一个布尔变量 valid
,您可以像这样在回调函数中使用它:
validatedInput = CheckMe( '0.5', 'TestBox' ); % Using the function to check input
if ~validatedInput
% Input wasn't valid!
myTextBox.String = '0';
end