Getting help for a function from another function

2 次查看(过去 30 天)
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

采纳的回答

Cedric
Cedric 2015-9-1
编辑:Cedric 2015-9-1
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.
  1 个评论
Patrick Mboma
Patrick Mboma 2015-9-2
Dear Cedric,
Thank you so much for this. While it does not solve the problem directly, at least it gives me some important directions I can follow, not just for this particular example but for some other applications.
Thanks a lot

请先登录,再进行评论。

更多回答(2 个)

the cyclist
the cyclist 2015-9-1
Journeyman solution: Copy & paste the help text from the top of myfunction into top of myfunction2.
  2 个评论
Patrick Mboma
Patrick Mboma 2015-9-1
Thanks The cyclist,
That is the current solution I have... The thing is that if I change something in the help of myfunction, I have to recopy and paste... I do not believe there is a solution other than the one you suggest but I had to ask to make sure there is no other way.
Cedric
Cedric 2015-9-1
An extended journeyman solution, if there is a large number of wrappers, is to build a tool which updates the help of wrappers automatically based on the help of wrapped functions.

请先登录,再进行评论。


Steven Lord
Steven Lord 2015-9-1
The lazy solution: make the help text of myfunction2:
myfunction2(...) is the same as myfunction(...) except [describe differences, if any]
The IMAGESC function does this, referring to IMAGE. It also lists IMAGE as a "See also" to encourage users to click on the hyperlink to access the help for IMAGE.
  1 个评论
Patrick Mboma
Patrick Mboma 2015-9-2
Dear Steven,
There are no differences in the behavior of the function except for the name. One is just an alias for the other. If you want a more concrete example, I could have a package and would like to write a shortcut to the function in the package.
Thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Functions 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by