# Variable Number of Input Arguments

2 visualizaciones (últimos 30 días)
Sai Hitesh Gorantla el 1 de Feb. de 2020
Editada: Walter Roberson el 29 de Mzo. de 2021
My code:
function [too_young] = under_age(age,limit)
if age<21
too_young = true;
else
too_young = false;
end
if age<limit
too_young = true;
else
too_young = false;
end
Getting error when executing
too_young = under_age(20):
Not enough input arguments.
Error in under_age (line 7)
if age<limit
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Ioannis Andreou el 1 de Feb. de 2020
Editada: Ioannis Andreou el 1 de Feb. de 2020
If you want variable number of inputs use nargin here
function too_young = under_age(age, limit)
if nargin < 2
limit = 21
end
...
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Más respuestas (4)

Bhaskar R el 1 de Feb. de 2020
You need provide two inputs but you have provided only one input 20. Give inputs functions as age and limit
[too_young] = under_age(15,21);
%Where 15 is age, 21 is limit
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
somnath paul el 15 de Ag. de 2020
In the instruction, it is clearly given that if limit is not given as a input argument then place the default value as 21.
If Code to call my function is
too_young = under_age(20)
Then the function should take the default value now the problem is how to place the default value for matlab.
function too_young = under_age(age,limit)
if nargin<2 && limit == 21
too_young = true
else
too_young = false
end
my answer in above but still they show me errors

Iniciar sesión para comentar.

somnath paul el 15 de Ag. de 2020
function too_young = under_age(age,limit)
if nargin == 1
% If number input argument is one then we will consider nargin as 1, that's why nargin == 1.
limit = 21;
if limit > age
too_young = true;
else
too_young = false;
end
end
if nargin == 2
% If number input argument is two then we will consider nargin as 2, that's why nargin == 2.
if limit > age
too_young = true;
else
too_young = false;
end
end
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Rik el 15 de Ag. de 2020
As you expressed in your comment, the reasoning is to set a value for the limit if it isn't provided. Therefore it doesn't make sense to duplicate the rest of the code as well.

Iniciar sesión para comentar.

PaaKwesi Anderson el 9 de Oct. de 2020
Editada: Walter Roberson el 29 de Mzo. de 2021
function too_young = under_age(age, limit)
if nargin<2
limit=21;
end
if age<limit
too_young = true;
else
too_young = false;
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
PaaKwesi Anderson el 9 de Oct. de 2020
Sorry Sir. Please should I delete it?
Fix the formatting? I don't really get you Sir
Rik el 9 de Oct. de 2020
If you don't have a good reason to keep this solution, yes, I think you should delete it.
For how to fix the formatting: have a read here.

Iniciar sesión para comentar.

Ganesh Vanave el 1 de En. de 2021
function too_young = under_age(age,limit)
if nargin == 1
limit = 21;
if limit > age
too_young = true;
else
too_young = false;
end
else nargin == 2
if limit > age
too_young = true;
else
too_young = false;
end
end
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Rik el 2 de En. de 2021
This is a suboptimal setup. You are repeating code, which means any change in algorithm will require you to remember to change two places.
Also, why did you post this answer? What does it teach?

Iniciar sesión para comentar.

### Categorías

Más información sobre Logical en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by