Getting help for a function from another function
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Patrick Mboma
am 1 Sep. 2015
Kommentiert: Patrick Mboma
am 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
0 Kommentare
Akzeptierte Antwort
Cedric
am 1 Sep. 2015
Bearbeitet: Cedric
am 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.
Weitere Antworten (2)
the cyclist
am 1 Sep. 2015
Journeyman solution: Copy & paste the help text from the top of myfunction into top of myfunction2.
2 Kommentare
Cedric
am 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.
Steven Lord
am 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.
Siehe auch
Kategorien
Mehr zu Functions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!