How to skip inputs in a function?

조회 수: 5 (최근 30일)
Naila Mohamed
Naila Mohamed 2022년 2월 23일
답변: Voss 2022년 2월 25일
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
David Hill
David Hill 2022년 2월 23일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Voss
Voss 2022년 2월 25일
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.

카테고리

Help CenterFile Exchange에서 Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by