- Make sure that the class definition files are on the MATLAB path. You can do this by adding the folder containing the class definition files to the MATLAB path using the addpath command or by setting the MATLAB path using the Set Path tool in the MATLAB menu.
- Define the properties of the nested class as public, so that they can be accessed from outside the class definition. For example, in the UTIL_tstValue class definition, you can define the dfNom and dfUseMin properties as follows:arduino
How to use nested object in Matlab function with non-constant properties
17 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
We have defined several nested classes to characterize manipulated objects. We are trying to use these classes in a Matlab function into a Simulink model. But we are encountered difficulties in using values of class objects.
First, an object of type of class definition, named DBCP_tstSwitch, is created and values are initializing. Object DBCP_tstSwitch is (partially) defined as follows:
classdef DBCP_tstSwitch
properties
stVds UTIL_tstValue = UTIL_tstValue
stIds UTIL_tstValue = UTIL_tstValue
These two properties are also class definition partially defined as follows:
classdef UTIL_tstValue
properties
dfNom double = 0
dfUseMin double = 0
Then, I cannot access to values contained in this object (e.g. DBCP_stSwitch.stVds.dfNom that is a double). For solving this issue, I tried two ways:
1/ Define properties of class DBCP_tstSwitch as constant. In this case, class object can be used in Matlab function but properties cannot be initialized to specific values (not editable because constant)
2/ Use coder.extrinsic for class definition. But it creates an object of type MxArray and I cannot access to nested object. For example, stVds is no more recoginzed as property of DBCP_tstSwitch.
Does anyone already encounter this issue and has a solution?
Thank you
0 Commenti
Risposte (1)
Jack
il 30 Mar 2023
It seems like you are trying to use a custom MATLAB class in Simulink, which can sometimes be challenging. Here are a few suggestions that might help you access the values contained in the nested objects:
classdef UTIL_tstValue
properties (Access = public)
dfNom double = 0
dfUseMin double = 0
end
end
Make sure that the class constructor initializes the nested objects to their default values. For example, in the DBCP_tstSwitch class definition, you can define the constructor as follows:
classdef DBCP_tstSwitch
properties
stVds UTIL_tstValue = UTIL_tstValue()
stIds UTIL_tstValue = UTIL_tstValue()
end
methods
function obj = DBCP_tstSwitch()
% Initialize nested objects
obj.stVds = UTIL_tstValue();
obj.stIds = UTIL_tstValue();
end
end
end
To use the class in a Simulink model, create an instance of the class using a MATLAB Function block. In the MATLAB Function block, you can access the properties of the class instance using standard MATLAB syntax. For example, to access the dfNom property of the stVds object, you can use the following code:
function y = fcn(DBCP_stSwitch)
y = DBCP_stSwitch.stVds.dfNom;
end
I hope this helps! If you continue to have issues, feel free to provide more details about your implementation and I will do my best to assist you.
Vedere anche
Categorie
Scopri di più su Simulink Functions in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!