필터 지우기
필터 지우기

Function Arguments Using Nargin

조회 수: 1 (최근 30일)
Amanda
Amanda 2013년 2월 27일
I want to control the arguments (data sets) in a function to plot. So if I push in only one data set (x1,y1) into the function, it will plot that data and not get an error that x2 is empty or cleared variable. Or if I push in two data sets (x1,y1,x2,y2) it will plot that. Here is the code:
%.........................
%testing data set
%.........................
x1 = [1 1 1 1 1];
y1 = [1 2 3 4 5];
x2 = [2 2 2 2 2];
y2 = [1 2 3 4 5];
%...........................
function plotinputs(x1,y2,x2,y2)
if nargin == 2
plot(x1,y1)
else nargin == 4
plot(x1,y1,x2,y2)
end
Thanks, Amanda
  댓글 수: 3
Muthu Annamalai
Muthu Annamalai 2013년 2월 27일
Amanda what you are looking at is called 'varargin' documented with usable examples http://www.mathworks.com/help/matlab/ref/varargin.html
You need to process the cell array 'varargin' according to the number of args, which you already know, is 'nargin'.
You have all the pieces right, and you only need the right 'varagin' bits.
Amanda
Amanda 2013년 2월 27일
I knew I was missing some pieces in the code. I will check out the reference. Thanks a lot.

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

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 27일
편집: Azzi Abdelmalek 2013년 2월 27일
There is an error
function plotinputs(x1,y2,x2,y2)
it should be
function plotinputs(x1,y1,x2,y2)

카테고리

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