unexpected value for nargout when returning a class/struct
조회 수: 2 (최근 30일)
이전 댓글 표시
What values for nargout would you expect for the last two lines in the run block: 0 or 1?
Class A
classdef A < handle
methods
function r=fctA(~)
disp(['nargout=' num2str(nargout)])
r=B;
end
end
end
Class B
classdef B < handle
methods
function fctB(~,~)
end
end
end
run
a=A;
a.fctA.fctB;
a.fctA.fctB(1);
댓글 수: 2
답변 (1개)
Philip Borghesani
2014년 6월 11일
편집: Philip Borghesani
2014년 6월 11일
In general you cannot index into the output of a function call in MATLAB.
You have found an under documented feature of dot indexing on objects and a bug in that feature. The ability to index into the output of a dot function call on MATLAB class objects leaked into a previous version. This ability has always existed for Java objects. Because this feature just worked in many places it has been frequently exploited in MATLAB code and removing it would be quite difficult making supporting it necessary. In general we believe that the following code is more conformant MATLAB code for calling a method and will have similar performance:
a=A;
Bobj=fctA(a);
fctB(Bobj);
Nargout in your example code should be 1 and this is fixed in a future version of MATLAB. Note that a.fctA().fctB also returns 1.
I have entered a bug report for the crash with a nested function but I would not expect that code to be legal MATLAB code in the foreseeable future.
Information stated here is not an official guarantee of support or statement of policy from MathWorks.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!