How to use nested object in Matlab function with non-constant properties

13 views (last 30 days)
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

Answers (1)

Jack
Jack on 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:
  1. 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.
  2. 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
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.
  1 Comment
CUER Romain
CUER Romain on 4 Apr 2023
Hello Jack,
Thank you very much for your quick answer!
Unfortunately, I tried the solution you proposed but without success. In all nested class definition, I render access to properties public and initialize nested objects as you suggest.
Then, I implemented a Matlab function containing the following code:
function y = fcn(DBCP_DatabaseComponent)
y = DBCP_stDatabaseComponent.astSwitch(3,1).stIds.dfMax;
end
To obtain DBCP_stDatabaseComponent, I used Simulink block “From Workspace”. Note that DBCP_tstDatabaseComponent is the super class in which DBCP_tstSwitch is used:
classdef DBCP_tstDatabaseComponent
properties (Access = public)
astSwitch DBCP_tstSwitch = DBCP_tstSwitch % Row array of switch structure class definition
end
end
However, Matlab returns following errorUnsupported input format for From Workspace block”
I also tried to directly use DBCP_stDatabaseComponent in a Matlab function in Simulink, but Matlab returns error: “Code generation only supports initial values of class type for constant properties. Property 'astSwitch' of class 'DBCP_tstDatabaseComponent' is not constant ». So, it corresponds to the error already returned when I tried solution 1 in problem description.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!