How to skip inputs in a function?
19 次查看(过去 30 天)
显示 更早的评论
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
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!