Is this a Possible MATLAB bug? (Further strange Behavior)
이전 댓글 표시
I have encountered a strange behavior while solving one of the problems in Cody. I have simplified the code into the following
function ans = junk(s)
try
s;
catch
999
end
This code gives an error if an output is requested (a=junk(3)), i.e it does not assign s to ans (it does not throw an error, though, within the try block, so adding the catch block has no effect).
The strange part is that if the semicolon is removed or replaced with a comma, it works correctly. Also, if any operation is performed on s (e.g. s+0) it works fine.
If the variable (ans) is invoked within the try block on a separate line after s, it works fine. Strange again, if (ans) is added on the same line as s with a semicolon or a comma, it jumps to the catch block, confirming no assignment in the semicolon case, but not for the comma case which worked fine before.
Any suggestions what causes this behavior? Am I missing something?
UPDATE:
Trying to avoid the try-catch block I tried a switch statement. I encountered further strange behavior of the 'ans' variable. Consider:
function ans = kkk(f,a)
switch nargin
case 1
f;
otherwise
999;
end
This time a=kkk(1) works fine, but when the input is a function handle it errors out
>> a=kkk(1)
a =
1
>> a=kkk(@plus)
Error in kkk (line 2)
switch nargin
Output argument "ans" (and maybe others) not assigned during call to "C:\Users\Dr. K. Hamed\Documents\MATLAB\kkk.m>kkk".
댓글 수: 2
Daniel Shub
2012년 8월 29일
Seems like a bug to me.
Oleg Komarov
2012년 8월 29일
I can't find it (and I might be wrong), but I think I read that using ans as an output variable will cause conflicts. I've been googling around for an hour but no results so far about the statement.
채택된 답변
추가 답변 (3개)
Oleg Komarov
2012년 8월 29일
편집: Oleg Komarov
2012년 8월 29일
Equivalently
function out = foo(in)
in;
end
will throw the same error.
If you call a = foo(10), the function tries to return a = out, but since the body of the function doesn't contain any assignment to out, it errors (as expected).
Removing the semicolon will still error in the case you call a = foo(10). It will however display in = 10 if I call foo(10).
If you add out after in (no semicolon), before the execution jumps to the catch, it will still display in, which won't happen if out comes before.
In brief, you have to keep in mind two things:
- Instructions are executed sequentially
- You can't show what doesn't exist (in the workspace)
댓글 수: 8
Khaled Hamed
2012년 8월 29일
ans is the automatic variable assigned when an output that isn't already stored in another variable gets evaluated without a variable for it to be assigned to.
In the code:
s = 5;
s;
There is no need for ans - every variable has a place to go.
In the code:
s = 5;
s + 2;
ans is required to store the result of s + 2
Oleg Komarov
2012년 8월 29일
편집: Oleg Komarov
2012년 8월 29일
@Khaled: you're right, there's something glitchy if you use ans as the output variable.
I would not recommend using ans as output variable.
I suspect the display() plays around with ans, thus removing the semicolon affects whether the error is thrown or not.
Sven
2012년 8월 29일
Ah... I'm surprised.
When inside a function there is the difference you describe. Ignore my comment which is only valid when running directly from the command line.
By any chance are you trying some tricks that will get better Cody scores? ;)
Khaled Hamed
2012년 8월 29일
Oleg Komarov
2012년 8월 29일
편집: Oleg Komarov
2012년 8월 29일
@Khaled: I already commented about that and it seems you missed the fact that I recognized the glitch (scroll above or http://www.mathworks.co.uk/matlabcentral/answers/46972#comment_96651)
However, I preferred not to alter my post to keep track of what should be expected and what happens (as you describe).
Khaled Hamed
2012년 8월 30일
편집: Khaled Hamed
2012년 8월 30일
Oleg Komarov
2012년 8월 30일
Don't worry and no need to apologize. After all, I was the first who didn't understand the issue at first.
Daniel Shub
2012년 8월 30일
I think this is a bug, but your MWE muddies the water. The bug has nothing to do with ans being used as an output. The best set of MWEs that I can come up with is the following functions
function junkA(s)
s;
who
end
function junkB(s)
try
s;
who
end
end
function junkC(s)
try
1;
who
end
end
The WHO reveals the critical difference
>> junkA(1)
Your variables are:
ans s
>> junkB(1)
Your variables are:
s
>> junkC(1)
Your variables are:
ans s
>>
Something happens in the try block that prevents s from being assigned to ans, but it is not a universal failure.
댓글 수: 1
Khaled Hamed
2012년 8월 30일
편집: Khaled Hamed
2012년 8월 31일
Matt Fig
2012년 8월 29일
1 개 추천
According to the documentation,
" The MATLAB software creates the ans variable automatically when you specify no output argument. "
Since this does not happen, I would say it is a bug.
카테고리
도움말 센터 및 File Exchange에서 National Instruments Frame Grabbers에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!