Using a private method to set a number of private properties

33 次查看(过去 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