Passing constant struct to entry point in Matlab Coder

10 views (last 30 days)
I am trying to prevent code generation for unused execution paths. The system I am trying to compile to C with Matlab Coder is parametrized with a nested params struct, which also includes the switches for the execution paths I am trying to exclude. A simple solution would be declaring the whole nested parameter struct as constant, like this
codegen systemCode -args {1, coder.Constant(params)}
but this does not seem to have any effect. It seems the params struct is still treated as a non-constant. I'm looking for a solution to this problem that does not involve pulling the control path switches out of the params struct.
Ludger Solbach
Ludger Solbach on 13 Jul 2015
Edited: Ludger Solbach on 13 Jul 2015
What's puzzling is that even if I go straight to line where the switch is used in the run() function, by changing
if obj.params.doThis
if coder.const(obj.params.doThis)
I get the error that the expression cannot be reduced to a constant. How is it that coder gets the idea that this value can ever change?

Sign in to comment.

Accepted Answer

Ryan Livingston
Ryan Livingston on 14 Jul 2015
Edited: Ryan Livingston on 14 Jul 2015
Typically when you want to filter some code from code generation, the best approach is to use In this case you could use the code:
a = cell({1,2,3}); % not supported by coder
out = 0;
to filter out the code that uses cell arrays.
There appear to be some limitations regarding tracking constants through objects with code generation. Another workaround you may consider is to implement a System Object. These are special MATLAB classes that have some properties which are designed to interact well with code generation.
The basic idea is that they maintain some state and implement a step method which performs the actual computation to be done, much like your run method. They also have the notion of nontunable properties which are special properties in the class that abide by certain rules (e.g. they must have 1 assignment with a constant value). Because of these rules, they can generally be treated as constants by the code generation software.
I changed sysFunc to a System Object:
classdef sysFunc < matlab.System
properties (Nontunable)
function obj = sysFunc(params)
obj.params = params;
methods (Access = protected)
function setupImpl(obj)
% Implement tasks that need to be performed only once,
% such as pre-computed constants.
function out = stepImpl(obj,in)
% Implement algorithm. Calculate out as a function of
% input in and discrete states.
out = in;
if obj.params.doThis
a = cell({1,2,3}); % not supported by coder
out = 0;
function resetImpl(obj)
% Initialize discrete-state properties.
You'll also need to change to sys.step(x) in sysWrapper. After doing this, I was able to generate code using:
params.doThis = 0; codegen sysWrapper -args {1, coder.Constant(params)}
Despite this, I would still recommend using to determine if you are in code generation mode or running in MATLAB. However, leveraging nontunable properties of System Objects may allow you to better propagate constants through object properties for code generation.
Lastly, in MATLAB you can go to the Editor tab and click the arrow under New and choose System Object->Advanced to get a template System Object that shows all of the supported behaviors you can override.
Jan Siegmund
Jan Siegmund on 18 Mar 2020
This works well with the original matlab coder, however in MATLAB HDL coder, the fixed-point converter step complains:
Property 'param' of class 'ExampleClass' is a structure type. Classes with structure properties are not supported.
I just basically want to achieve the same thing:
Excluding not supported code paths by using a constant struct.
Using the coder.Constant input class does also not work for the fixed point converter.

Sign in to comment.

More Answers (0)


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!