Effacer les filtres
Effacer les filtres

Getting help for a function from another function

2 vues (au cours des 30 derniers jours)
Patrick Mboma
Patrick Mboma le 1 Sep 2015
Commenté : Patrick Mboma le 2 Sep 2015
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

Réponse acceptée

Cedric
Cedric le 1 Sep 2015
Modifié(e) : Cedric le 1 Sep 2015
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 commentaire
Patrick Mboma
Patrick Mboma le 2 Sep 2015
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

Connectez-vous pour commenter.

Plus de réponses (2)

the cyclist
the cyclist le 1 Sep 2015
Journeyman solution: Copy & paste the help text from the top of myfunction into top of myfunction2.
  2 commentaires
Patrick Mboma
Patrick Mboma le 1 Sep 2015
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 le 1 Sep 2015
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.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 1 Sep 2015
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 commentaire
Patrick Mboma
Patrick Mboma le 2 Sep 2015
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by