我有一个包含大约 10 个输入参数的函数。数据处理后,这些参数可以是空数组/向量。在这种情况下,我想将每个输出设置为 0 并退出(返回)该函数。
我如何进行检查以确保函数的所有输入都是非空的,而不必逐个键入它们。我想要类似的东西。
function [outputs1and2] = myfunct(many_arguments)
if isempty(any_input_argument)
out1 = 0;
out2 = 0;
return
end
out1 = some_math;
out2 = more_math;
end
最佳答案
您可以使用 varargin
最初将所有输入参数保存在 cell array 中,您可以使用 cellfun
轻松检查:
function [out1, out2] = myfunct(varargin)
if any(cellfun(@isempty, varargin))
out1 = 0;
out2 = 0;
return
end
% ...further processing
end
当您需要使用输入参数进行后续处理时,您可以像这样从 varargin
中提取它:
in1 = varargin{1}; % Get first input argument
此外,虽然它可能比你要找的更复杂,但你也可以查看inputParser
objects。用于验证函数的输入参数。
关于matlab - 检查 Matlab 中没有输入参数为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45595321/