5

是否可以用不使用异常的代码替换以下代码?手柄x是提供的手柄。我想在使用前测试它的有效性(有实际的代码来支持句柄)。

x = @notreallyafunction;
try
   x();
catch 
   disp('Sorry function does not exist.');
end
4

1 回答 1

8

要测试函数句柄,例如筛选出问题中的虚假句柄x=@notreallyafunction,您可以使用functions命令检查句柄并获取引用函数的名称、类型(简单、嵌套、重载、匿名等)以及位置(如果是)在文件中定义。

>> x = @notreallyafunction;
>> functions(x)
ans = 
    function: 'notreallyafunction'
        type: 'simple'
        file: ''
>> x = @(y) y;
>> functions(x)
ans = 
     function: '@(y)y'
         type: 'anonymous'
         file: ''
    workspace: {[1x1 struct]}
>> 

functions内置函数句柄(例如)的输出x=@round看起来就像一个虚假的函数句柄(typeis 'simple')。下一步是测试命名函数是否存在:

>> x = @round;
>> fx = functions(x)
fx = 
    function: 'round'
        type: 'simple'
        file: ''
>> exist(fx.function)
ans =
     5
>> x = @notreallyafunction;
>> fx = functions(x)
fx = 
    function: 'notreallyafunction'
        type: 'simple'
        file: ''
>> exist(fx.function)
ans =
     0

但是,您需要处理匿名函数,因为它们未通过存在测试:

>> x = @(y) y;
>> fx = functions(x)
>> exist(fx.function)
ans =
     0

解决方法是先检查type. 如果type'anonymous',则检查通过。如果type不是,您可以 'anonymous'依靠它们exist来检查函数的有效性。总结起来,你可以创建一个这样的函数:

% isvalidhandle.m Test function handle for a validity.
%   For example,
%     h = @sum; isvalidhandle(h) % returns true for simple builtin
%     h = @fake; isvalidhandle(h) % returns false for fake simple
%     h = @isvalidhandle; isvalidhandle(h) % returns true for file-based
%     h = @(x)x; isvalidhandle(h) % returns true for anonymous function
%     h = 'round'; isvalidhandle(h) % returns true for real function name
%   Notes:  The logic is configured to be readable, not compact.
%           If a string refers to an anonymous fnc, it will fail, use handles.
function isvalid = isvalidhandle(h)

if ~(isa(h,'function_handle') || ischar(h)),
    isvalid = false;
    return;
end

if ischar(h)
    if any(exist(h) == [2 3 5 6]),
        isvalid = true;
        return;
    else
        isvalid = false;
        return;
    end
end

fh = functions(h);

if strcmpi(fh.type,'anonymous'),
    isvalid = true;
    return;
end

if any(exist(fh.function) == [2 3 5 6])
    isvalid = true;
else
    isvalid = false;
end
于 2013-10-10T23:19:12.407 回答