How to replace statements in Matlab functions for code generation?

3 views (last 30 days)
I would like to have optional code in a functions which is used for autocoding. This should be similar to preprocessor/compiler directives in C/C++. For example:
%#if CODEGEN
a = someFunctionNotCodeGenerationCapable(b);
%#else
a = simplifiedFunctionCodeGenerationCapable(b);
%#endif
This would allow to have a simulation in Matlab with more sophisticated functions e.g. GUI which may prevent code generation.
When code is generated it should be replace by another function for which code can be generated,

Answers (1)

Steven Lord
Steven Lord on 20 Oct 2023
Are you hoping to do code replacement? Or are you thinking of something smaller like checking coder.target to see if you're in MATLAB or generating code for a specific target?
  2 Comments
Stephan Theil
Stephan Theil on 20 Oct 2023
I want to generate code from this function but the function call of "someFunctionNotCodeGenerationCapable" does not allow it. So when autocode is generated the statement a = simplifiedFunctionCodeGenerationCapable(b); shall be used and the other call shall be ignored.
The hint with coder.target sounds promising. I will check it out.
Many thanks.
Stephan Theil
Stephan Theil on 20 Oct 2023
I checked it in my application but it does not work. I guess the reason is that it is not a plain Matlab function but an initialization callback of a masked subsystem.
There is a masked subsystem. The mask has three parameters. The first is a selection from a list (popup).
Depending on the first parameter the last parameter is not needed. Therefore it is disabled. That is convenient when running simulations in Matlab.
In our process we first create a referenced subsystem and test a protected model.
When running the same simulation with a referenced subsystem containing the masked subsystem we get the error "Cannot change property 'Enabled' of *** while simulation is running".
If I remove the statement to disable/enable the parameter it works. But then I loose the functionality when doing normal simulations.
But the following solution does not work when running as a referenced subsystem.
if coder.target('MATLAB')
param.Enabled = 'off';
end

Sign in to comment.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!