Search code examples
matlabanonymous-functionundocumented-behavior

set 'help' for matlab anonymous functions


I have a long anonymous function, and I wondered whether the help output can be modified (easily):

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

I know long anonymous functions might be a rather unusual thing- nevertheless: can this be achieved, maybe with undocumented functions, for only as long as the function handle exists?

Edit: commenter asked for a use-case: I read up on anoynmous functions with muiltiple outputs (here Lorem on the art of matlab) such as

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

And I want to make sure I remember what the second output argument is, later in time, you know... because humans forget stuff


Solution

  • You can create your own anonymous function handling class which would mimic this functionality, shadowing the help function for this object type only.

    I've written the class below, but will show usage first, it simply requires having the class on your path and slightly adapting the way you declare anonymous functions:

    We can override the subsref function for this class type also, then you can call the function handle directly using () syntax, rather than indexing into a structure as suggested by Nicky's answer.

    Note that you have to pass the handle in, not the function name (i.e. help(f) or f.help, not help f or help('f')). You'd have to fully shadow the help function to get around this limitation, which I wouldn't really endorse!

    Usage

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

    Default function handle help is preserved if not specified

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

    Class code

    The class could use some extra input checking etc, but works in principle!

    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