When calling this function how is the input error caught

1 view (last 30 days)
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

Accepted Answer

Mehmed Saad
Mehmed Saad on 18 Apr 2020
Edited: Mehmed Saad on 18 Apr 2020
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 Comments
Darnell Gawdin
Darnell Gawdin on 18 Apr 2020
hahahahah, Doh!. Yes of course. Thanks! :) Sorry its been a long day.
Walter Roberson
Walter Roberson on 18 Apr 2020
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!