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
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)
orf.help
, nothelp f
orhelp('f')
). You'd have to fully shadow thehelp
function to get around this limitation, which I wouldn't really endorse!Usage
Default function handle
help
is preserved if not specifiedClass code
The class could use some extra input checking etc, but works in principle!