How to use property from one class in to define a property in another class?

Hi All,
I have defined one class
classdef AgentVars
properties
set_o = linspace(-0.2,0.2,20) % set of observations
end
end
I want to use the the set_o property in another class. For example
classdef Agent
properties
set_o = AgentVars.set_o
end
end
However, currently I am not able to access a property from the other class. How can I do that? Thank you

 Accepted Answer

I don't know what the 'correct' answer would be, but you can also set the value in the constructor:
classdef Agent
properties
set_o = NaN
end
methods
function obj=Agent
obj.set_o=AgentVars.set_o
end
end
end
You could also inherit from a superclass, but that only works if you want to inherit from a single class.

8 Comments

Thanks. But when I run it, the value of set_o is still NaN. It should be the value that was assigned in the AgentVars class. How can I get that?
Please post the exact code you're using and attach the classdef m-files.
Here are the two classes I am using. I want to access the set_o from the AgentVars class to use it for the Agent class. When I try to run it, this is the error I get.
The property 'set_o' in class 'AgentVars' must be accessed from a class instance because it is not a Constant property.
Looking at your code structure it makes most sense to inherit the parameters, making AgentVars a superclass. Is there any reason you don't?
Alternatively, you could do as the error message suggests: make the properties in AgentVars Constant.
After a few more edits (putting calculated properties in the constructor, it runs without error:
a=Agent
a =
Agent with properties: set_o: [-0.2000 -0.1789 -0.1579 -0.1368 -0.1158 -0.0947 -0.0737 -0.0526 -0.0316 -0.0105 0.0105 0.0316 0.0526 0.0737 0.0947 0.1158 0.1368 0.1579 0.1789 0.2000] kappa: 0.0800 sigma: 0.0400 c_t: 1 beta: 100 agent: 1 lambda: NaN eval_ana: 1 alpha: 0.1000 task_agent_analysis: NaN q_0_0: 0.5000 o_t: NaN d_t: NaN a_t: NaN r_t: NaN pi_0: NaN pi_1: NaN E_mu_t: 0.5000 v_a_0: NaN v_a_1: NaN v_a_t: NaN p_a_t: NaN t: NaN p_o_giv_u: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] p_o_giv_u_norm: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] C: NaN q_o: NaN q_1: NaN d: NaN G: NaN p_d_t: [NaN NaN] Q_t: [2×2 double] product: [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] mu: [100×100 double] p_mu: [1×100 double] mu_for_ev: [100×1 double]
Thanks for this. I have not made AgentVars into a superclass because I was not aware of this method. I will try it. Could you please explain how to make the properties a constant.
I figured how to make it a constant. Could please help me understand what are calculated properties?
As far as I know that isn't an official term. Take a look at the file I attached: the values of some properties are calculated based on the values of other properties. Because those aren't yet initialized, they can't be accessed in the properties block. That is why they are calculated in the constructor.

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!