为matlab匿名功能设置“帮助”

时间:2018-12-10 13:38:53

标签: matlab anonymous-function undocumented-behavior

我有一个很长的匿名函数,我想知道是否可以轻松地修改帮助输出:

>> myfunc=@(x) x; %example anonymous function
>> help myfunc
myfunc is a variable of type function_handle.

我知道很长的匿名函数可能是一件很不寻常的事情-尽管如此:仅在函数句柄存在的情况下,能否使用未公开的函数来实现?

编辑:评论者询问了一个用例:我阅读了具有多个输出(此处为Lorem on the art of matlab)的无用函数,例如

fmeanVar = @(x) deal(mean(x), var(x));
%a long example function to split a cell array containing 3D matrices into two cell arrays 
myfunc=@(x,c) deal(cellfun(@(d) d(:,:,1:c:end),x),cellfun(@(d) d(:,:,setxor(1:c:end,1:end)),x));

我想确保我记得第二个输出参数是什么,稍后,您知道...因为人类忘记了东西

3 个答案:

答案 0 :(得分:5)

问题在于,当您调用help时,它将重新读取文件。当您使用

创建匿名函数时
f = @(x) x %Sample text

然后它忽略%Sample text,因此消失了。一种解决方案是将其转换为结构,其中一个字段是函数,另一个字段是帮助。例如。像

fMeanVar.fct = @(x) [mean(x), var(x)];
fMeanVar.help = "Second output is the variance"

因此,当您要使用调用的函数时

fMeanVar.fct([1,2,3,4])

如果您忘记了用法,可以直接致电

fMeanVar.help

答案 1 :(得分:5)

您可以创建自己的匿名函数处理类,该类将模仿此功能,仅遮盖此对象类型的help函数。

我已经在下面编写了该类,但将首先显示用法,它只需要在您的路径上包含该类,并稍微调整声明匿名函数的方式即可。

我们也可以为此类类型重写subsref函数,然后您可以使用()语法直接调用函数句柄,而不是按照建议的by Nicky's answer索引到结构中。< / p>

请注意,您必须传递句柄,而不是函数名称(即help(f)f.help,而不是help fhelp('f'))。您必须完全遮盖help函数才能解决此限制,我对此并不表示赞同!

用法

>> f = anon( @() disp( 'Hi!' ), 'This function displays "Hi!"' );
>> help( f )
Input is a value of type function_handle.
This function displays "Hi!"
>> f()
Hi!

>> f = anon( @(x) x + 10, 'Adds 10 to the input' );
>> help( f )
Input is a value of type function_handle.
Adds 10 to the input
>> f(15:17)
ans = 
  [ 25, 26, 27 ]

>> f.func = @(x) x + 15;
>> f.helpStr = 'Adds 15 to the input'
>> f(15:17)
ans = 
  [ 30 31 32 ]

如果未指定,则保留默认功能句柄help

>> f = anon( @(x) x + 10 );
>> help( f )
Input is a value of type function_handle.

类代码

该类可以使用一些额外的输入检查等,但是原则上可以工作!

classdef anon < handle
    properties ( Access = public )
        helpStr % String to display on help( obj )
        func    % Function handle (meant for anonymouse functions
    end
    methods
        function obj = anon( func, helpStr )
            assert( isa( func, 'function_handle' ) ); % Input check            
            obj.func = func;
            if nargin > 1
                obj.helpStr = helpStr; % Set help string
            end
        end
        function help( obj )
            h = help( obj.func ); % Normal behaviour.
            if ~isempty( obj.helpStr )
                % Custom string (does nothing if empty)
                fprintf( '%s%s\n', h, obj.helpStr );   
            else
                disp( h );
            end
        end
        function varargout = subsref( obj, s )
            % Need to override the subsref behaviour to enable default
            % function calling behaviour!
            switch s(1).type
                case '()'
                    [varargout{1:nargout}]  = obj.func( s(1).subs{:} );
                otherwise
                    [varargout{1:nargout}]  = builtin('subsref', obj, s);
            end
        end
    end
end

答案 2 :(得分:2)

根据help的Matlab文档,不可能:

  

帮助名称显示按名称指定的功能的帮助文本,例如函数,方法,类,工具箱或变量。

不是句柄或符号。