防止matlab函数显示输出

时间:2018-02-05 16:33:48

标签: matlab

我不知道为什么Matlab不这样做,但如果没有为函数分配输出参数,我宁愿我的函数不输出到控制台。

E.g。

function out=getmagic(n)
    out=magic(n);
    figure; 
    plot(out(1,:));
end

对于高数字,如果在调用函数时忘记行末尾的;标记,则会越来越烦人。到目前为止,我的解决方案是在函数末尾包含if语句:

if nargin==0 %no output argument is requested
    out=[]; %shorten ouput argument to prevent flooding of console
end

有没有更好的方法(例如根本不提供任何输出)?

1 个答案:

答案 0 :(得分:3)

我通常会这样做:

function varargout = getmagic(n)

    out=magic(n);
    figure; 
    plot(out(1,:));

    if nargout>0
        varargout{1} = out;
    end

end

现在,如果你用输出参数调用它,你将获得out的值;但如果你在没有输出参数的情况下调用它,你将什么也得不到。