In Simulink I would like to use a "classdef object" in stead of a struct to initialise a bus signal

4 views (last 30 days)
This user defined class:
classdef s_class
properties
a=7
end
end
And a corresponding bus signal in Simulink:
elems = Simulink.BusElement;
elems.Name = 'a';
s_Bus = Simulink.Bus;
s_Bus.Elements = elems;
To initialise this bus signal (for instance in a Constant block) in Simulink we can use struct('a',7).
Is it possible to use s_class in stead?

Accepted Answer

Wim Vaassen
Wim Vaassen on 21 Jun 2023
Hi Animesh,
I understand that initialization with s_class is not possible in Simulink, although from the outside it still seems to contain the same data (type and value) as in a corresponding struct s. Maybe something for a next version.
In any case, thanks for your quick response.
Greeting,
Wim.

More Answers (1)

Animesh
Animesh on 21 Jun 2023
Unfortunately, it is not possible to use s_class directly as an initialization for a Simulink bus signal.
Bus signals in Simulink require elements to be defined as structures. While s_class is a class definition in MATLAB and a class object instantiated from s_class would not be directly compatible with Simulink busses.
You will need to convert the instance/object of s_class into a struct to use it as an initial value for a bus signal.
One way to achieve this is by defining a method in your s_class that returns an equivalent struct object.
For example, you could add the following method to your s_class definition:
methods
function s = asStruct(obj)
s.a = obj.a;
end
end
Then, you could initialize the bus signal like this:
s_obj = s_class; % Create object of s_class
busInitVal = s_obj.asStruct; % Convert object to struct
The busInitVal struct can then be used as the initial value of the bus signal in Simulink.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!