MATLAB Answers

only accept odd numbered input arguments

7 views (last 30 days)
Ahmed Abdulla
Ahmed Abdulla on 29 May 2020
Edited: Adam Danz on 1 Jun 2020
I am writing a function and I would like it to only run if the input argument is odd, is there an easy to specifcy that the input must be odd.

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 29 May 2020
Edited: Adam Danz on 1 Jun 2020
nargin counts the number of input arguments for a function.
mod(x,2) returns 1 when x is odd.
if mod(nargin,2)~=1
error('There must be an odd number of inputs.')
end

  0 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!