Given a named function, how can I call this function, and not the subfunction of the same name?
Mostrar comentarios más antiguos
My function is passed the name of a function (as a string). It just so happens that my function contains a subfunction of that name. I want to make sure I call the external function, instead of the subfunction. How can I achieve this?
E.g. save the following in test.m:
function test
feval(evalin('base', 'str2func(''help'')'), 'help');
end
function varargout = help(varargin)
error('Should not get here!');
[varargout{1:nargout}] = deal([]);
end
Then calling test gives:
>> test
Error using test>help (line 6)
Should not get here!
Error in test (line 2)
feval(evalin('base', 'str2func(''help'')'), 'help');
1 comentario
Oliver Woodford
el 29 de Mayo de 2015
Respuesta aceptada
Más respuestas (2)
Philip Borghesani
el 29 de Mayo de 2015
Editada: Philip Borghesani
el 29 de Mayo de 2015
There is a much simpler solution to this:
function fh=test
fh=str2func('@(x) help(x)');
fh('help')
end
...
1 comentario
Oliver Woodford
el 30 de Mayo de 2015
Titus Edelhofer
el 29 de Mayo de 2015
0 votos
Hi Oliver,
although this is not an answer to your question it might help anyway: this is one of the reasons to use function handles instead of strings denoting functions. The big advantage of a function handle is, that the function is determined in the moment the function handle in contrast to strings, where in the moment of evaluation the dispatching happens.
If you pass @help instead of 'help' to your function as input, you are sure, that the correct function is used.
Titus
5 comentarios
Oliver Woodford
el 29 de Mayo de 2015
Titus Edelhofer
el 29 de Mayo de 2015
Hm, so renaming the subfunctions to something highly unlikely to be of the same name as outside function does not work either :(.
Titus Edelhofer
el 29 de Mayo de 2015
Oliver,
what about this:
feval(evalin('base', '@(x) help(x)'), 'help')
This works fine. The "only" drawback: you need to know the number of input and output variables to build up the string ...
Titus
Oliver Woodford
el 29 de Mayo de 2015
Editada: Oliver Woodford
el 29 de Mayo de 2015
Alfonso Nieto-Castanon
el 29 de Mayo de 2015
Editada: Alfonso Nieto-Castanon
el 29 de Mayo de 2015
couldn't you use:
feval(evalin('base','@(varargin) help(varargin{:})'),'help')
to account for variable number of inputs?
Categorías
Más información sobre Functions en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!