Matlab 函数,用于检查无效值

Matlab 函数,用于检查无效值,matlab,matlab-guide,Matlab,Matlab Guide,我正在为我的Matlab指南程序编写一个函数。我想对指南中的3个文本框应用一个从0到1的限制,并且它只能是数字。(如果用户输入的值无效,则应生成一个错误框)。问题是我想为此编写一个函数,而不是在每个文本框的回调中编写限制代码。用户也不必一次输入所有值,而是当用户输入三个值中的任何一个并生成反馈时,函数应该运行。我写的函数如下,但它不起作用。(没有必要将所有三个输入都提供给函数,这就是我在输入之间使用| |的原因) 函数检查(maxMBT | | minMBT | | mainMBT) max_M

我正在为我的Matlab指南程序编写一个函数。我想对指南中的3个文本框应用一个从0到1的限制,并且它只能是数字。(如果用户输入的值无效,则应生成一个错误框)。问题是我想为此编写一个函数,而不是在每个文本框的回调中编写限制代码。用户也不必一次输入所有值,而是当用户输入三个值中的任何一个并生成反馈时,函数应该运行。我写的函数如下,但它不起作用。(没有必要将所有三个输入都提供给函数,这就是我在输入之间使用| |的原因)

函数检查(maxMBT | | minMBT | | mainMBT)
max_MBT=str2double(get(hObject,'String');
如果isnan(maxMBT)| | maxMBT<0 | | maxMBT>1
errordlg(“MBT的最大值无效。请输入0到1之间的值”);
设置(最大值,'String',0);
如果isnan(minMBT)| | minMBT<0 | | minMBT>1
设置(最小值,'String',0);
errordlg(“MBT的最小值无效。请输入介于0到1之间的值”);
如果isnan(mainMBT)| | mainMBT<0 | | mainMBT>1
设置(edtMBT,‘字符串’,0);
errordlg(“MBT的值无效。请输入介于0到1之间的值”);
结束
结束
结束

您的语法错误,传递可选参数时没有将它们分开。相反,我建议使用两种输入:

  • 输入要检查的值
  • 输入此值的“类型”,具体取决于触发的回调
  • 该函数将如下所示:

    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
    

    不能定义这样的函数。语法是
    函数[out_args=](arg1[,arg2[…])
    ,方括号之间的位是可选的。你要做的是定义一个进行检查的函数,并从三个回调函数中的每一个调用该函数。请Wolfie帮助我解决这个问题,因为我是一个初学者。我已经帮助你留下了一个答案,其中的代码你几乎可以直接在你的项目中使用,并对它的工作原理进行了评论。。。你还想要什么?查看您删除的答案,这些答案应该是问题编辑,错误非常清楚-MBT应该是什么?应该是字符串。是的,你真的解释得很好。我试着这么做,但结果是错误的。thsi synta可以吗?boxType=set(handles.boxType,'String',MBT)?不,您试图将
    boxType
    设置为等于
    set
    命令的输出,但
    set
    命令没有输出。。。你在那条线上想干什么?我怀疑您只是想要类似于
    boxType=get(handles.MyTextBox,'String')
    的东西,但是我不知道
    handles
    是什么。正如您所说的“boxType应该是回调指定的字符串,以标识框”。我只是想将MBT保存为字符串,这样就可以将它传递给您给定的functin。那我该写什么呢?
    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
    
    validatedInput = CheckMe( '0.5', 'TestBox' ); % Using the function to check input
    if ~validatedInput
        % Input wasn't valid!
        myTextBox.String = '0';
    end