필터 지우기
필터 지우기

When calling this function how is the input error caught

조회 수: 1 (최근 30일)
Darnell Gawdin
Darnell Gawdin 2020년 4월 18일
댓글: Walter Roberson 2020년 4월 18일
This is a test function from the documentation. If I include a letter in the arguments instead of a number matlab reports an error. My question is how does matlab know its an error?
testValues(5,1,2,3,4,5,6,7,8,9,10,a)
function testValues(threshold,varargin)
minInputs = 2;
maxInputs = Inf;
narginchk(minInputs,maxInputs);
for k = 1:(nargin-1)
if (varargin{k} > threshold)
fprintf('Test value %d exceeds %d\n',k,threshold)
end %endif
end %endfor
end %end function
Thanks for any input, haha input get it!
D

채택된 답변

Mehmed Saad
Mehmed Saad 2020년 4월 18일
편집: Mehmed Saad 2020년 4월 18일
since you include a letter a which Matlab think is a variable. so it try to pass the variable a to testValues but that variable doesnot exist so Matlab give you error
either you initialize the variable a
a =1;
testValues(5,1,2,3,4,5,6,7,8,9,10,a)
or you can use string to pass the letter
testValues(5,1,2,3,4,5,6,7,8,9,10,'a')
  댓글 수: 2
Darnell Gawdin
Darnell Gawdin 2020년 4월 18일
hahahahah, Doh!. Yes of course. Thanks! :) Sorry its been a long day.
Walter Roberson
Walter Roberson 2020년 4월 18일
right. A letter would be a variable name or function name, and matlab would do name resolution. If it were a variable then the value of the variable would be passed down, and you would only be able to tell that apart from numeric constant by examining argname(). If it were a function name (not anonymous function as those are variables or expressions) then it would try to evaluate the function with no parameters and pass the first output into the testValues.
MATLAB would never look and say "Ah hah, you passed a name here instead of a numeric constant!". You can probe that with argname if you really want but argname cannot tell the difference between you passing in 10 and you passing in a+0 where a happens to contain 10: both show up with argname empty.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by