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

3 次查看(过去 30 天)
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?

采纳的回答

Wim Vaassen
Wim Vaassen 2023-6-21
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.

更多回答(1 个)

Animesh
Animesh 2023-6-21
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.

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by