How to use property from one class in to define a property in another class?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Rik
on 6 Aug 2021
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
Prashanti Ganesh
on 6 Aug 2021
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?
Rik
on 6 Aug 2021
Please post the exact code you're using and attach the classdef m-files.
Prashanti Ganesh
on 6 Aug 2021
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]
Prashanti Ganesh
on 6 Aug 2021
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.
Prashanti Ganesh
on 6 Aug 2021
I figured how to make it a constant. Could please help me understand what are calculated properties?
Rik
on 6 Aug 2021
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.
Prashanti Ganesh
on 9 Aug 2021
Thanks so much. It makes sense now.
More Answers (0)
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)