How to force Embedded coder to use a specific struct name (instead of struct_xxxxxx) for system object parameters?

10 views (last 30 days)
I have a custom System object that uses a configuration struct returned by a helper function:
classdef Demo_setpoint_adjuster < matlab.System
properties
config = Demo.getconfig;
end
methods
function obj = Demo_setpoint_adjuster()
% Optionally initialize config/state here if needed
end
end
methods(Access = protected)
function [adjusted_setpoint] = stepImpl(obj, setpoint)
% Default passthrough implementation
% Assign outputs
adjusted_setpoint = setpoint * obj.config.gain;
% TODO implement
end
function num = getNumInputsImpl(~), num = 1; end
function varargout = getNumOutputsImpl(~), varargout = {1, []}; end
function varargout = getOutputSizeImpl(~), varargout = {1}; end
function varargout = getOutputDataTypeImpl(~), varargout = {'double'}; end
function varargout = isOutputComplexImpl(~), varargout = {false}; end
function varargout = isOutputFixedSizeImpl(~), varargout = {true}; end
end
end
My getconfig function looks like this:
function cfg = getconfig(varargin)
cfg = Simulink.Bus.createMATLABStruct('DEMO_config_def');
cfg.gain = 10;
cfg.offset = 1e-13;
coder.cstructname(cfg, 'my_struct');
end % end of main function getconfig
When I generate code with Embedded coder, the header file defines the parameter type as something like:
typedef struct {
real_T gain;
real_T offset;
} struct_6h72eH5WFuEIyQr5YrdGuB;
Instead of my desired:
typedef struct {
real_T gain;
real_T offset;
} my_struct;
Even though I use coder.cstructname, Simulink still generates the anonymous struct_xxxxxx
Question:
When using a System object parameter inside a simulink model, how can i ensure embedded coder generates a struct with a specific typedef name (e.g., my_struct)? Is the recommended approach to use Simulink.Bus/Simulink.Parameter, or can coder.cstructname be applied directly in this workflow?

Answers (1)

Goutam
Goutam on 12 Sep 2025
Edited: Goutam on 12 Sep 2025
Hi Manoj,
Embedded Coder ignores coder.cstructname for tunable System-object properties, so the easiest supported solution is to model the structure as a Simulink.Bus / Simulink.Parameter and let the bus name (or its alias) become the typedef.
If the structure is compile-time constant, you can keep everything in MATLAB and coder.cstructname will work.
Here is a workaround that you may try :
1. Use a Bus-based workflow
  • Create a Simulink.Bus object DEMO_config_def in the base workspace that matches the fields gain / offset.
  • In the model, use a Simulink.Parameter whose DataType is 'Bus: DEMO_config_def'.
  • In the Code Mappings editor (or via set_param(model,'GenerateBusDefinition','on')) set “Generate typedef for bus objects” to On and pick the desired name (‘my_struct’, or keep the bus name itself).
2. Define the structure as a constant property so that coder.cstructname can be applied automatically
  • coder.cstructname is honored only when the structure is treated as a compile-time constant.
  • If you declare the System-object property as Constant (or as a persistent coder.const) you can force that:
classdef Demo_setpoint_adjuster < matlab.System
properties(Constant)
config = coder.const(Demo.getconfig)
end
...
end
and in getconfig keep
coder.cstructname(cfg,'my_struct');
3. Use a data-type alias
  • If the randomly generated name works fine but you'd like something more readable, you can create a Simulink.AliasType in the Data Dictionary or base workspace to give it a clearer alias
T = Simulink.AliasType;
T.BaseType = 'struct_6h72eH5WFuEIyQr5YrdGuB'; % generated name
my_struct_typ = 'my_struct';
assignin('base', my_struct_typ, T);
  • Set the parameter datatype to this alias in Code Mappings.
  • Embedded Coder will still generate the anonymous struct, but immediately after it you will see
typedef struct_6h72eH5WFuEIyQr5YrdGuB my_struct;
Kindly refer to the documentation links below for reference:
Hope this helps.
Regards,
Goutam

Community Treasure Hunt

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

Start Hunting!