필터 지우기
필터 지우기

Different input variables depending on case within a switch ?

조회 수: 5 (최근 30일)
ABDULAZIZ ALTUN
ABDULAZIZ ALTUN 2020년 6월 12일
편집: Rik 2020년 6월 12일
I am writting a function and one of the input of this function is a switch variable.
Depending on the case of the switch this function needs to take in different inputs to get the output from the case. Could this be done in Matlab or I should write 5 different functions?
%% Test: function description
function z = Test(key)
switch key
case 1
% inputs are a and c
z=a+c;
case 2
% inputs are b and f
z=b*f;
end
end
As you can see for case 1, I need the function Test to take a and c hence
z=Test(key, a,c)
For case 2 I want it
z=Test(key, b,f)
How can I do this using nargin, nargout, varagin?

답변 (1개)

Rik
Rik 2020년 6월 12일
편집: Rik 2020년 6월 12일
You can do this with nargin, nargout, varargin, and/or varargout.
Edit:
Note that this example does no input checking and will not result in a clear error if the number of arguments is incorrect.
%% Test: function description
function z = Test(key,varargin)
switch key
case 1
% inputs are a and c
[a,c]=deal(varargin{:});
z=a+c;
case 2
% inputs are b and f
[b,f]=deal(varargin{:});
z=b*f;
end
end
  댓글 수: 1
ABDULAZIZ ALTUN
ABDULAZIZ ALTUN 2020년 6월 12일
편집: Rik 2020년 6월 12일
@Rik I have updated my question can you please provide this particular example?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by