在MATLAB中,您可以在一个.m
文件中拥有多个功能.当然有main函数,然后是嵌套函数或本地函数.
每种功能类型的示例:
% 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
% ----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
使用函数end
语句时,差异很明显.但是,我不认为你没有清楚地记录你使用的是什么,因为这是有效的语法:
% myfunc.m with some other function
function myfunc()
disp(myotherfunc()); …
Run Code Online (Sandbox Code Playgroud)