从命令行调用本地函数 [英] Calling local functions from command line
问题描述
我在m-文件.例如:
% begining of public_function.m file
function fh = public_function( )
%
% do some computation...
fh = @local_function; % return function handle to local function defined below
function y = local_function( x )
%
% a local function inside public_function.m file
%
% some manipulation on x
y = x;
% end of public_function.m file NOTE THAT local_function is NOT nested
现在,我想从命令行(而不是从public_function
)调用local_function
.我能够使用从public_function
返回的函数句柄做到这一点:
Now, I would like to call local_function
from command line (and not from public_function
). I was able to do so using the function handle returned from public_function
:
>> fh = public_function(); % got handle to local_function
>> y = fh( x ); % calling the local function from command line :-)
我的问题:
除了从显式传递函数句柄之外,还有其他方法可以从命令行(或其他m文件/函数)调用本地函数吗?
My question:
Is there any other way (apart from explicitly pass the function handle) to call local function from command line (or other m-file/functions)?
更确切地说,我想要一种方法来访问文件中的任何本地函数(只要我知道它的名称).因此,如果我有public_function.m
文件(和功能),并且知道该文件本地的local_function
,是否可以从命令行访问local_function
?
More precisely, I want a method to access any local function in a file (provided that I know its name). So, if I have public_function.m
file (and function) and I know that local_function
is local to that file, is there a way to access local_function
from command line ?
推荐答案
官方文件说:
...您不能从命令行或其他文件中的函数调用本地函数.
... you cannot call a local function from the command line or from functions in other files.
因此,您必须将其句柄传递给调用方,以便允许在其m文件之外间接调用它.我相信,否则,没有任何记录的明智方法可以访问本地功能.
According to this, you must pass its handle to the caller in order to allow invoking it indirectly outside its m-file. I believe that there is no documented sensible way to access local functions otherwise.
奇怪的是,您仍然可以使用help
来完成此操作:
Oddly though, you can still do this with help
:
help public_function>local_function
这篇关于从命令行调用本地函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!