3

I have a function with about 10 input arguments. After data processing, these arguments can be empty arrays/vectors. In this event, I would like to set each output to 0 and exit(return) from the function.

How can I do a check to make sure that all inputs to a function are nonempty, without having to type each of them out. I would like something like.

function [outputs1and2] = myfunct(many_arguments)
if isempty(any_input_argument)
    out1 = 0;
    out2 = 0;
    return
end
out1 = some_math;
out2 = more_math;
end

1 Answer 1

3

You could make use of varargin to initially hold all of your input arguments in a cell array, which you can easily check using cellfun:

function [out1, out2] = myfunct(varargin)
  if any(cellfun(@isempty, varargin))
    out1 = 0;
    out2 = 0;
    return
  end
  % ...further processing
end

And when you need to use an input argument for subsequent processing, you can just extract it from varargin like so:

in1 = varargin{1};  % Get first input argument

In addition, although it may be more involved than what you're looking for, you can also check out inputParser objects for validating input arguments to your function.

Sign up to request clarification or add additional context in comments.

2 Comments

I see this as a possibility, though having to later do varargin{} for each of the inputs makes it about the same amount of lines. Ill probably just specify isempty for each one. Thanks for your answer.
@Leo: You don't have to place every value in varargin into a new variable. You can just use, for example, varargin{1} in place of every usage of in1, or whatever you had previously called your first input variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.