How to skip inputs in a function?

21 vues (au cours des 30 derniers jours)
Naila Mohamed
Naila Mohamed le 23 Fév 2022
Réponse apportée : Voss le 25 Fév 2022
I have a function: y= signal(T,T_set,P,I, D, N)
There's a variable x which equals to T- T_set.
If the length of x equals 1, I need to skip the D and I terms of the function.
This is what I have so far:
function y= signal(T,T_set,P,I, D, N)
x= T- T_set
k=length(x)
if k==1
How do I proceed to skip D and I?
  1 commentaire
David Hill
David Hill le 23 Fév 2022
Don't understand your question. You don't have to use the inputs in your function and certainly don't have to use them in any order.

Connectez-vous pour commenter.

Réponses (1)

Voss
Voss le 25 Fév 2022
If you mean that you want to be able to call the function with on or the other of two different sets of input arguments, one set being T,T_set,P,I,D,N (in that order) and the other set being T,T_set,P,N (in that order) (and whether the length of x = T-T_set equals 1 is how you can determine which set of inputs is being given), then you can do something like this:
function y= signal(T,T_set,P,I, D, N)
x= T- T_set
k=length(x)
if k==1
% in this case, the fourth input argument "I"
% is actually the value of N
N = I;
end
But if that's what you want to do it's probably clearer to use the nargin() function to tell you the number of input arguments given, in which case the dependence on length(x) == 1 is lost:
function y= signal(T,T_set,P,I, D, N)
if nargin == 4
% in this case, the fourth input argument "I"
% is actually the value of N
N = I;
end
It's hard to know if using nargin() instead of length(x) == 1 is a good idea here without knowing more about what the function is supposed to do and how it's supposed to be used.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by