我不知道为什么 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/