是否有可能在没有尝试块的情况下测试函数句柄

是否有可能在没有尝试块的情况下测试函数句柄

本文介绍了是否有可能在没有尝试块的情况下测试函数句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is is possible to replace the following code with something which does not use exceptions? The handle x is a provided handle. I want to test it for validity (having actual code to back the handle) before use.

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


推荐答案

处理,比如用于在你的问题中筛选出伪造的 x = @ notreallyafunction ,你可以使用命令来检查句柄并获取被引用函数的名称,类型(简单,嵌套,重载,匿名等),以及在文件中定义的位置。

To test function handles such as for screening out the bogus x=@notreallyafunction in your question, you can use the functions command to check the handle and get the referenced function's name, type (simple, nested, overloaded, anonymous, etc.), and location if it is defined in a file.

>> 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]}
>>

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

The output of functions for a handle to a builtin (e.g. x=@round) will look just like a bogus function handle (type is 'simple'). The next step is to test the named function for existence:

>> 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

然而,你需要处理匿名函数,因为他们失败的存在测试:

However, you need to deal with anonymous functions since they fail existence test:

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

解决方案是首先检查类型。如果 type 'anonymous',那么检查通过。如果类型不是 'anonymous',他们可以依赖检查函数的有效性。总结一下,你可以创建一个这样的函数:

The solution is to first check the type. If type is 'anonymous', then the check passes. If the type is not 'anonymous', they you can rely on exist to check the function's validity. Summing up, you could create a function like this:

% 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

这篇关于是否有可能在没有尝试块的情况下测试函数句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:50