How to pass multiple parameters in Matlab coder?

3 views (last 30 days)
I developed two algorithms, let's call them EncoderClass.m and DecoderClass.m, that share their configuration (Configuration.m), which is a struct of structs (100~ Matlab system objects parameters, more or less). I wrote two functions:
% Encode.m
function [tx] = Encode(config, data)
% Decode.m
function [rx] = Decode(config, data)
and they look like that:
function [tx] = Encode(config, data) %#codegen
assert(isa(config, 'struct');
assert(isa(data, 'double'));
assert(length(data) == config.length);
e = EncoderClass(config);
tx = step(e, data);
And I would run it as follows:
data = genRandomData()
config = MyNewCoolConfig1
codegen Encode -args { coder.Constant(config), data } ...
Decode -args { coder.Constant(config), data }
After realizing that structs are not supported as constant in codegen (cannot remeber where I read it), I am having a hard time figuring out how to have a central file with my configuration and passing it somehow to my two Encoder and Decoder classes.
EncoderClass and DecoderClass have several Matlab objects in their belly, that's the reason why I need to have all those parameters and all at once.
Moreover, I'd like to have the possibility to have two different instances of EncoderClass and DecoderClass, so I cannot use globals (without touching Encoder and Decoder classes code at least), so I can write an export function as:
data1 = genRandomData()
data2 = genRandomData()
config1 = MyNewCoolConfig1
config2 = MyNewCoolConfig2
codegen Encode1 -args { coder.Constant(config1), data1 } ...
Decode1 -args { coder.Constant(config1), data1 } ...
Encode2 -args { coder.Constant(config2), data2 } ...
Decode2 -args { coder.Constant(config2), data2 }
The only solution I can think of is using a long list of parameters and passing it to the class constructor, which will should have enough intelligence to know to whom pass what.
I don't like this solution, so, is there – call it a best practice – a way to achieve what I am doing in a Matlab flavour?

Answers (1)

Raghu Boggavarapu
Raghu Boggavarapu on 26 Nov 2021
MATLAB Coder allows generating code with constant input configs. Starting with R2021b single MEX can be generated for multiple input argument input types, please refer to Generate Code for Multiple Entry-Point Functions - MATLAB & Simulink (


Find more on MATLAB 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!