How do I generate code from a MATLAB script that uses preprocessor defines from external files with Embedded Coder R2023a?

I am generating code using Embedded Coder from a MATLAB script in R2023a. This code uses values from preprocessor defines in external header files. I would like to be able to generate code that uses these external defines, similar to the 'ImportedDefine' storage class for Simulink models. However, despite this being an option in Simulink, there is no such option for MATLAB code using storage classes as described here:
.
Is there a good way to generate this kind of code?

 Akzeptierte Antwort

Unfortunately, this is a current limitation of Embedded coder for MATLAB scripts. However, there is a workaround using the 'coder.opaque' function as described here:
.
This function allows you to declare a variable in MATLAB that is treated as a black-box value with no type or initial value in generated code. There are limitations to how such variables can be used in MATLAB code. Here is a snippet of code that demonstrates how you can get around these limitations:
function y = externalDefine()
y = getX;
end
function x = getX()
if isempty(coder.target)
% This branch executes in pure MATLAB, when code generation is not used and C includes are not available.
x = int32(100);
else
coder.inline('always');
coder.cinclude('global.h');
% coder.opaque tells MATALB Coder that this value is a black box, its type is
% int (does not matter here) and its initial value is X.
% Since we want to use x in MATLAB code, it needs to have some MATLAB type.
% I chose int32 here for convenience.
x = int32(coder.opaque('int', 'X'));
end
end
In this case, the function 'externalDefine' implements the desired functionality, while the 'getX' function obtains the desired value. When ran in MATLAB, 'getX' returns the value 100. In the generated code, however, 'getX' will return the externally defined value 'X'.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by