How do I implement typedefs of unions + structure variables in Simulink?

18 views (last 30 days)
How can I implement a union + structure variable in Simulink so that when I generate code I will get a variable structure with shared access?
I want to duplicate the behaviour of the C-code below so I can use typedef struct in several similar data sets.
%Code using C
typedef struct st_module_data{
struct st_FaultInfo{
union{
uint8 AllFaults;
struct {
uint8 OVP :1;
uint8 OVW :1;
uint8 UVP :1;
uint8 UVW :1;
uint8 OCP :1;
uint8 OCW :1;
uint8 OTP :1;
uint8 OTW :1;
}
}
}Fault;
struct st_MeasuredData{
uint8 voltage = 0;
uint8 current = 0;
uint8 temperature = 0;
}Measured;
};
st_module_data s_module_01;
s_module_01.Fault.AllFaults = 0; % Clears all flags
s_module_01.Fault.OVP = 1; % Sets only the OVP flag
s_module_01.Measured.voltage = ui8_ADC_5V; % Sets the measured voltage to 5V
I was able to recreate the structure and "typedef"-ish callbacks in matlab by using bus editor:
But I am unable to get the union thing working and can only access the lowest heirarchy of the structure - e.g.:
s_module_01.Fault.AllFaults = 0; % How do I implement this?
s_module_01.Fault = 0; % Does not work...
s_module_01.Fault.OVP = 1; % OK - works in Matlab Functions, Simulink and Stateflow
s_module_01.Measured.voltage = ui8_ADC_5V; % OK - works in Matlab Functions, Simulink and Stateflow

Answers (1)

Prateekshya
Prateekshya on 27 Sep 2023
Hi Paul,
As per my understanding you want to implement "union" data structure in Simulink. Simulink does not support direct use of such a data structure. However, there is some workaround available for the same. You can follow this link for more information: https://in.mathworks.com/matlabcentral/fileexchange/116520-use-union-datatype-in-simulink
I hope this helps!

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!