matlab - 阻止 matlab 函数显示输出

标签 matlab

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

例如

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

有没有更好的方法来做到这一点(例如根本不会给出任何输出)?

最佳答案

我通常会做这样的事情:

function varargout = getmagic(n)

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

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

end

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

关于matlab - 阻止 matlab 函数显示输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48627175/

相关文章:

matlab - 为什么在Matlab中发生有关gcc的错误?

c++ - 将 Vector 拆分为 block - 奇怪的结果

macos - MATLAB 遇到内部错误,需要关闭

Matlab 使用日期向量对日期和时间序列进行向量化实现

c++ - 如何解决 "Undeclared identifier error" block 中的 "if"?

matlab - R 或 matlab 中精确召回曲线下的面积

windows - 从命令行运行 m 文件时如何隐藏 "MATLAB Command Window"?

matlab - 如何从matlab将不同长度和不同数据类型的数据写入文本文件

arrays - 在 MATLAB 2015 中的图形数组中查找 GraphicsPlaceholder

用于逼近指数函数的 Matlab 代码