calling dependent properties from one class to another

Hi guys,
I want to call dependent properties from one class to another in matlab OOP. Could anyone suggest me how to do that.
I just want to call all my dependent properties. Since i calculated them using get function iam unable to understand how to call them. Could anyone give me some idea.
Thankyou

1 commentaire

Are you trying to access the dependent properties through the class name or through a class instance?
The people class defines properties height and weight and a dependent property bodyMassIndex calculated from height and weight.
It doesn't make sense to ask for the bodyMassIndex for the people class.
It makes sense to ask for the bodyMassIndex of a specific instance of the people class.
If that's not the difficulty you're experiencing trying to access dependent properties, please show a small example that simulates the real problem you're trying to solve/model and explain the difficulty using that small example.

Connectez-vous pour commenter.

Réponses (2)

Matt J
Matt J le 10 Sep 2020
Modifié(e) : Matt J le 10 Sep 2020
You access it the same as you would a non-Dependent property,
object.property

5 commentaires

Hi Matt,
thankyou for your reply. I tried but when iam trying to call them in my fit function iam getting reference to no-existent field 'property name'
Well, there's no code for us to examine together...
classdef clCore
properties(GetAccess=?fitFunction)
depth
length
Matcore
Wm
end
properties(Dependent)
area
end
methods
function obj=clCore(depth,length,Matcore)
obj.depth=depth;
obj.length=length;
obj:Matcore=Matcore;
end
function area=get.area(obj)
area=obj.depth*obj.length
end
end
end
objCore=clCore(4,5,1)
classdef Designspec
properties
Matcore
Wm=1
dxma=1.5;
end
methods
function obj=Designspec(Matcore)
obj.Matcore=Matcore;
end
end
end
objDS=Designspec(1);
classdef fitFunction
methods(Static)
function f=fit(objDS)
P=[0.456,0.2030]
objCore.Matcore=objDS.Matcore;
objCore.Wm=objDS.Wm;
% my core parameters have to be updated
objCore.depth=P(1);
objCore.length=P(2);
% i need to call my dependent properties
c1=lte(objCore.depth,objDS.dxma)
end
end
end
objfit=fitFunction;
f=objfit.fit(p;objDS)
function [c]=lte(x,xmxc)
% lte less than or equal to function. Returns 1 if the
% argument is less than or equal to the allowed value,
% otherwise it returns a value between 0 and 1.
%
% Call:
% c=lte(x,xmxc)
%
% Inputs
% x = value to be tested
% xmxc = maximum value of x which passes constraint
%
% Output
% c = constraint value
if (x>xmxc)
c=1/(x-xmxc+1);
else
c=1;
end
This is a simple example but i still have lot of parameters. i just explained with 2 to 3 parameters. My question is in class fitFunction i want to access dependent parameters from clCore when i was trying to do that it always says reference to non-existenet field so i tried giving
function f=fit(objDS,objCore)
instead of
function f=fit(objDS)
It worked but i need to give this fitfunction in Genetic algorithm where i could use only objDS as input but not objCore.
here is my genetic algorithm program
classdef cl_UI_GA
properties
dv_act=0;
rp_lvl=1;
ev_pp=true;
mg_nreg=10;
end
methods(Static)
function [obj,fP,GAS,bi,bf]=GA1(objDS) % set up GA
ngen=50;
npop=50;
obj=gapdefault(2,0,npop,ngen);
% gene description
% min max chrm chrm par
% val val type id # description
obj.gd= [ 1 4 1 1; ... % depth
1 2 1 1] % 2 length
for i=1:2
objDS.Wm = i;
for k = 1:3
objDS.Matcore=k;
% objT.Tm=1;
%FOIL:
if objDS.Wm==2
%Design Space for Foil Winding Model:
obj.gd= [obj.gd;
1e-2 1 3 1; ... % 11 hf - foil winding height(m)
0.1*1e-3 3*1e-3 3 1]; ...% 12 wf - foil winding width(m)
% conduct optimization
[fP,GAS,bi,bf]= gaoptimize(@fitFunction.fit,obj,objDS);
% save the results
if objDS.Matcore==1
s2='IndFoil_';
s3='Steel_';
filename=strcat(s2,s3);
save (filename)
else
disp('No such material catalog for core');
end
%LITZ:
else
% go to Litz fit
end
end
objGAP=cl_UI_GA;
[objGAP,fP,GAS,bi,bf]=objGAP.GA1(objDS)
and when i am trying to run the program my algorithm is not converging it is just showing some constant value for the number of genertions i have generations. It could be 50 or 100 my fitness value is not changing.
Could anyone help me.
Thankyou
So, it's a different problem now? Earlier you said you were getting an error message about a non-existent property reference.
i described about the different cases of my error. i have also explained about the non-existent field property reference and also the way i tried.

Connectez-vous pour commenter.

classdef fitFunction
methods(Static)
function f=fit(objDS)
P=[0.456,0.2030]
objCore.Matcore=objDS.Matcore;
objCore.Wm=objDS.Wm;
% my core parameters have to be updated
objCore.depth=P(1);
objCore.length=P(2);
% i need to call my dependent properties
c1=lte(objCore.depth,objDS.dxma)
end
end
end
You created an object objCore in the base workspace. This function does not operate on that object. The assignments to objCore make a struct array in this function's workspace. So if you were to ask for objCore.area in this function, that struct doesn't have a field by that name.
There's a scientist named Steven Lord at the SETI Institute. Even though we share a name, I am not him and he is not me. Similarly just because the object in the base workspace and this identifier in the function share a name, they are not the same thing.
If you want this function to operate on an objCore object, it needs to accept that object as an input or to create one. [Technically there is a way for it to "reach into" its caller's workspace or the base workspace, but I strongly discourage doing that sort of "action at a distance".]

Catégories

En savoir plus sur Foundation and Custom Domains dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by