相关疑难解决方法(0)

当不使用"end"时,一个.m文件中的多个函数是嵌套的还是本地的

在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)

matlab function nested-function

8
推荐指数
1
解决办法
424
查看次数

标签 统计

function ×1

matlab ×1

nested-function ×1