calling a nested function in matlab

3 views (last 30 days)
Osama Alkurdi
Osama Alkurdi on 13 Feb 2020
Answered: Walter Roberson on 13 Feb 2020
there is a note in a matlab documentation said that "you have to call the nested functions in a certain ways and not by others".
I used the ways in the red below and nothing goes wrong!
can anyone explain to me what is the documentation mean?

Answers (1)

Walter Roberson
Walter Roberson on 13 Feb 2020
feval() and str2func() only look for functions in the "top level" namespace -- the functions with corresponding .m or .slx or .mdl or .mlapp or .mex* files. They are effectively executed inside the base workspace, not inside the workspace of the executing function. feval() and str2func() will not (reliably) find nested functions or functions that are private to the current file.
In practice what this means is that you should restrict yourself to function handles built with @ and not pass the names of functions to other routines as character vectors or strings, except in some cases where passing a specific function name is a documented option to the function. For example, cellfun(@length, X) and cellfun('length', X) are both valid and have slightly different meanings. See the "backwards compatibility" section of the cellfun documentation.




Community Treasure Hunt

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

Start Hunting!

Translated by