在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这样的函数是本地函数还是嵌套函数?

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐