필터 지우기
필터 지우기

Using a private method to set a number of private properties

조회 수: 31 (최근 30일)
I'm trying to work with OOP concepts in MATLAB but run into the following problem: I have a number of coefficients which need to be computed, based on some public properties inside a private class method. However, these properties are not set for some reason I don't quite understand. The method I have defined as:
methods (Access = private)
function obj = calcSchemeCoefficients (obj)
obj.lambda2 = (obj.gamma2*obj.k^2)/obj.h^2;
obj.mu = (obj.kappa*obj.k)/obj.h^2;
obj.mu2 = obj.mu^2;
obj.zeta = (4*obj.b2*obj.mu)/obj.kappa;
obj.den = 1+obj.b1*obj.k;
obj.a10 = (2-2*obj.lambda2-6*obj.mu2-obj.zeta)/obj.den;
obj.a11 = (obj.lambda2+4*obj.mu2+0.5*obj.zeta)/obj.den;
obj.a12 = -obj.mu2/obj.den;
obj.a20 = (-1+obj.zeta+obj.b1*obj.k)/obj.den;
obj.a21 = (-0.5*obj.zeta)/obj.den;
% this prints the correct result
fprintf('\nlambda2: %f\n', obj.lambda2);
end
end
However when I try to recall the property lambda2 for use in another (public) function, it doesn't seem to contain any value... I suspect I am missing some logic here, but I really have no clue what it could be.
Any help on this would be greatly appreciated

채택된 답변

Daniel Shub
Daniel Shub 2011년 11월 28일
My guess is you are not handling the returned object correctly. Are you using a value class or a handle class? What happens when you do:
obja.lambda2
objb = calcSchemeCoefficients(obja)
objb.lambda2
obja.lambda2
  댓글 수: 1
Michael Dzjaparidze
Michael Dzjaparidze 2011년 11월 28일
You are right. I did not realize I had to assign the 'output' of calcSchemeCoefficients to obj again. so: obj = obj.calcSchemeCoefficients(); does the job actually. Thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by