Hej, I am trying to build a function with control over 3 arguments. However, I have detected a possible bug on R2018b.
The nargin control works perfectly for any number but 3!
Here you are a minimal example.
function y = myfunction(x1,x2,x3)
if nargin < 3
x3=false;
if nargin < 2
x2 = 0;
end
y = x1 + x2;
if x3
y = y/norm(y);
end
end
end

 채택된 답변

per isakson
per isakson 2020년 4월 28일
편집: per isakson 2020년 4월 28일

1 개 추천

The example works as expected for me. It throws an error because no value is assigned to y
>> y = myfunction( 1, 2, true )
Output argument "y" (and maybe others) not assigned during call to "myfunction".
There is no else clause of the outer if-statement.
/R2018b

추가 답변 (1개)

John D'Errico
John D'Errico 2020년 4월 28일

0 개 추천

How is this a bug? Well, a bug in your code.
What do you expect to happen for 3 arguments? We see this test:
if nargin < 3
So if nargin ==3, then NOTHING inside the if executes. But that if statement controls the entire code inside your function.
Therefore the problem is when you call the function with 3 arguments, the output argument will never even be defined.
That is not a bug in MATLAB however. Not in any version of MATLAB.

댓글 수: 1

Aldo Díaz
Aldo Díaz 2020년 4월 29일
Sorry John for not being so good as you wished, but please don't go online crucifying people!

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

카테고리

도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2020년 4월 28일

댓글:

2020년 4월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by