Effacer les filtres
Effacer les filtres

Using a private method to set a number of private properties

33 vues (au cours des 30 derniers jours)
Michael Dzjaparidze
Michael Dzjaparidze le 28 Nov 2011
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

Réponse acceptée

Daniel Shub
Daniel Shub le 28 Nov 2011
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 commentaire
Michael Dzjaparidze
Michael Dzjaparidze le 28 Nov 2011
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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by