MATLAB: Getting help for a function from another function

functionhelp

Dear all,
suppose I want to get help from a function. I can simply type h=help('myfunction). But then suppose that I have another function, say myfunction2 which is identical to myfunction. As an example myfunction2 could be as follows
function [a,b,c]=myfunction2(varargin)
[a,b,c]=myfunction(varargin{:});
end
It is clear that myfunction and myfunction2 will have the same behavior. The question is, if I want to get help for myfunction2, is it possible to get it from myfunction?
Thanks

Best Answer

As mentioned in my comment above, you could write a small code manager tool which performs this kind of updates for you. I often do this when my projects get large, e.g. for spotting files with some variable names that I would like to update (and updating them), for spotting files with specific types of TODOs, etc. Here is an example that took 10 minutes to develop. It could be greatly improved, but it shows an alternative to copying by hand:
classdef CodeManager < handle
properties
funcs
patterns
end
methods
function obj = CodeManager()
% - Predefine array of wrappers/wrapped functions.
obj.funcs = {} ;
obj.addFunc( 'A2', 'A1' ) ;
obj.addFunc( 'B2', 'B1' ) ;
% - Predefine patterns.
obj.patterns.help = '^\s*%.*?(?=^\s*[^%\s])' ;
end
function addFunc( obj, wrapper, wrapped )
fId = numel( obj.funcs ) + 1 ;
obj.funcs{fId} = {wrapper, wrapped} ;
end
function updateWrappersHelp( obj )
for fId = 1 : numel( obj.funcs )
obj.updateWrapperHelp( obj.funcs{fId}{:} ) ;
end
end
function updateWrapperHelp( obj, wrapper, wrapped )
fprintf( 'Help %s -> %s\n', wrapped, wrapper ) ;
% - Get raw help of wrapped function.
wrappedContent = fileread( [wrapped, '.m'] ) ;
wrappedHelp = regexp( wrappedContent, obj.patterns.help, ...
'match', 'once', 'lineanchors' ) ;
% - Update wrapped function name with wrapper.
wrappedHelp = strrep( wrappedHelp, lower(wrapped), lower(wrapper) ) ;
wrappedHelp = strrep( wrappedHelp, upper(wrapped), upper(wrapper) ) ;
% - Update wrapper help.
wrapperContent = fileread( [wrapper, '.m'] ) ;
wrapperContent = regexprep( wrapperContent, obj.patterns.help, ...
wrappedHelp, 'once', 'lineanchors' ) ;
% - Overwrite wrapper M-File (create an _updated version for
% the tests).
fId = fopen( [wrapper,'_updated.m'], 'w' ) ;
fwrite( fId, wrapperContent ) ;
fclose( fId ) ;
end
end
end
With this code saved in CodeManager.m and the four functions A1/2 and B1/2 (all attached), you can update v2's (wrappers) as follows: first you create the code manager:
cm = CodeManager() ;
and then you call
cm.updateWrappersHelp()
so all wrappers/wrapped functions declared in the constructor are processed, or just e.g.
cm.updateWrapperHelp( 'B2', 'B1' )
if you want to update specifically B2.m based on B1.m.
Spending another hour on it, you could add calls to WHICH to get wrappers/wrapped paths, improve pattern matching and name replacement, insert/extract extra information (like date of last update), etc.
PS: I made the updater output new files A2_updated.m and B2_updated.m instead of overwriting originals, so you can repeat the tests.