Naming Variables wit using varargin.

조회 수: 12 (최근 30일)
Bgr Mrt
Bgr Mrt 2017년 10월 31일
댓글: Stephen23 2021년 1월 9일
Hello everyone. I define a Gama function which must had 4 inputs. If we entered more than 4 inputs it should say Error!. I must use the variables that taken from inside of the varargin to an equation like this fun= (sin(2*pi.*f.*(m-n).*T))/ 2*pi.*f.*(m-n).*T; but how can i name my variables? For example if i entered Gama(1,3,5,7) the first value should represents m ,the second value should represents n and the others represents f and t. How can i name them and use it into my function. Thanks for your help. Have a Good Day.
function Gama(varargin)
if nargin<=4
disp("Number of input arguments: " + nargin)
disp(varargin)
else
'Error'
f=varargin{1}
m=varargin{2}
n=varargin{3}
T=varargin{4}
if nargin<=4
fun= (sin(2*pi.*f.*(m-n).*T))/ 2*pi.*f.*(m-n).*T;
t=1:1:100;
figure
plot(fun,t)
xlabel('Omega')
ylabel('Gamma')
else
'Error! More Than 4 Inputs entered'
end
  댓글 수: 1
Stephen23
Stephen23 2021년 1월 9일
Why do academics insist on teaching such bizarre, unrealistic, and ugly practices? This should simply be:
function Gama(f,m,n,T)
Using named arguments directly is simpler, more efficient (no allocating data to new variables), still throws an error for too many input arguments, and MATLAB shows the variable names when calling the function:
Whereas using varargin shows nothing useful at all:

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 31일
Minor modifications are needed
function Gama(varargin)
if nargin < 4
error('Not enough inputs entered')
elseif nargin > 4
error('Too many inputs entered')
end
f=varargin{1}
m=varargin{2}
n=varargin{3}
T=varargin{4}
T=1:1:100;
fun= (sin(2*pi.*f.*(m-n).*T))/ 2*pi.*f.*(m-n).*T;
figure
plot(T, fun)
xlabel('Omega')
ylabel('Gamma')
I would, however, recommend that you consider the possibility that you instead want
fun= (sin(2*pi.*f.*(m-n).*T)) ./ (2*pi.*f.*(m-n).*T);
  댓글 수: 1
Bgr Mrt
Bgr Mrt 2017년 11월 1일
Thank you for your response. Have a good day.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by