在MATLAB中,您可以在一个.m文件中拥有多个函数.当然有主要功能,然后是
nested or local functions.
每种功能类型的示例:
% myfunc.m with local function ------------------------------------------
function myfunc()
disp(mylocalfunc());
end
function output = mylocalfunc()
% local function, no visibility of variables local to myfunc()
output = 'hello world';
end
% -----------------------------------------------------------------------
% myfunc.m with nested function -----------------------------------------
function myfunc()
disp(mynestedfunc());
function output = mynestedfunc()
% nested function, has visibility of variables local to myfunc()
output = 'hello world';
end
end
% ----------------------------------------------------------------------
使用函数的结束语句时,差异很明显.但是,我不认为你没有清楚地记录你使用的是什么,因为这是有效的语法:
% myfunc.m with some other function
function myfunc()
disp(myotherfunc());
function output = myotherfunc()
% It's not immediately clear whether this is nested or local!
output = 'hello world';
有没有明确定义像myotherfunc这样的函数是本地函数还是嵌套函数?