Help with commands varargin and switch

[EDIT: Mon May 16 20:51:23 UTC 2011 - Reformat - MKF]
Hello,
I am new to MATLAB and I am trying to understand the code given in the following paper (Matov et al 'Analysis of microtubule dynamic instability using a plus-end growth marker', Nature, 7, 761-768 (2010).
One of the codes uses command varargin and switch. Could someone please explain the use of these command in MATLAB.
Part of the code is given below %START
function [cutoffIndex, cutoffValue, sp, axesH] = cutFirstHistMode(varargin)
% goodDataIdx is used in case data contains nans
goodDataIdx = [];
% check for axesHandle
if ishandle(varargin{1})
axesH = varargin{1};
varargin(1) = [];
verbose = 1;
else
axesH = [];
end
switch length(varargin) - isscalar(varargin{end})
case 1 % data
doHistogram = 1;
data = varargin{1};
data = data(:);
%END. I have given only a part of this code.
Your help is truly appreciated.
Thanks.

 채택된 답변

Matt Fig
Matt Fig 2011년 5월 16일

1 개 추천

Probably the best thing is for you to read the doc on these topics.

댓글 수: 4

NS
NS 2011년 5월 16일
Thanks Matt.
So does varargin include all the inputs in your function? If this is the case varargin{1} will be the first and varargin{end} is the last. Am I correct?
Also, could you explain 'if ishandle(varargin{1})' in the code. I always assumed the if operator to have a proportionality sign like "< > =" etc.
Thanks.
Jan
Jan 2011년 5월 16일
IF needs a logical expression as argument. The relational operators like ">" or "==" (not "=" !) reply a logical value, e.g. "2 > 3" replies FALSE. The function ISHANDLE replies a logical value also.
You can simply try what VARARGIN does: Let Matlab explain it itself by doing.
NS
NS 2011년 5월 18일
Thanks Jan.
I ran the code. The "if" operator processed the true statements if the argument had a positive value (not 0).
eg. when ishandle(varargin{1})=1 the if statement was processed and when the value was 0 the else statements were processed.
Also I understood what varargin stands for after running the code. Thanks once again
Jan
Jan 2011년 5월 18일
Be careful: "ishandle(varargin{1})=1" is not a comparison, but a standard bug. You want: "ishandle(varargin{1})==1". "if ishandle(varargin{1})" works also, because ISHANDLE replies a LOGICAL already.
"if X" with a numerical X (even an array!) means explicitely: "if all(X) && ~isempty(X)".

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

추가 답변 (0개)

카테고리

질문:

NS
2011년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by