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