⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validarg.m

📁 有关matlab的电子书籍有一定的帮助希望有用
💻 M
字号:
function [var,err] = validarg(str,range,strict,labelstr)
%VALIDARG Evals string in base workspace and returns variable if the result
%   is a real double scalar within the specified range.
%   Puts up a dialog with an error message if there is one.
%   Inputs:
%     str - string containing expression to evaluate in base workspace
%     range - 2-elt vector specifying a real double scalar range
%     strict - 2-elt logical vector specifying whether the inequality
%        is strict at the corresponding endpoint.  Use 1 for strict
%        inequality, 0 otherwise.  i.e. range = [3 4], strict = [1 0]
%        means a valid variable has 3 < x <= 4. 
%     labelstr - name of the object being read in, eg 'numerator'
%   Outputs:
%     var - contains data on success
%     err - 0 means no error, 1 means error

%   Copyright (c) 1988-98 by The MathWorks, Inc.
%       $Revision: 1.6 $


    if isempty(str)
        var = [];
        err = 1;
        return
    end

    arb_obj = {'an arbitrary' 'object'};
    var = evalin('base',str,'arb_obj');
    if isequal(var,arb_obj)
        errordlg1(['The ' labelstr ' that you entered ' ...
                  'couldn''t be evaluated.'],'Error')
        var = [];
        err = 1;
    elseif ~strcmp(class(var),'double')
        errordlg1(['The ' labelstr ' that you entered ' ...
                  'is not of class ''double''.'],'Error')
        var = [];
        err = 1;
    elseif prod(size(var)) ~= 1
        errordlg1(['The ' labelstr ' that you entered ' ...
                  'is not a scalar.'],'Error')
        var = [];
        err = 1;
    elseif ~isreal(var)
        errordlg1(['The ' labelstr ' that you entered ' ...
                  'is not a real scalar.'],'Error')
        var = [];
        err = 1;
    elseif var > range(2) | var < range(1)
        errordlg1(['The ' labelstr ' that you entered ' ...
                  'is outside the permissible range of [' ...
                   num2str(range(1)) ',' num2str(range(2)) '].'],...
                  'Error')
        var = [];
        err = 1;
    elseif strict(1) == 1 & var == range(1)
        errordlg1(['The ' labelstr ' must be strictly greater than '...
                   num2str(range(1)) '.'],'Error')
        var = [];
        err = 1;
    elseif strict(2) == 1 & var == range(2)
        errordlg1(['The ' labelstr ' must be strictly less than '...
                   num2str(range(2)) '.'],'Error')
        var = [];
        err = 1;
    else
        err = 0;
    end

function errordlg1(str,title)

   f =  msgbox(str,title,'error','modal');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -