Initialize Simulink's "MATLAB System" block for codegen with unsupported codegen functions at "setup time"

9 Ansichten (letzte 30 Tage)
I would like to be able to call unsupported codegen functions (like exist() or evalin()) within a MATLAB System object at setup time so that I can initialize all of the objects necessary internal state. I would like the resulting system object to run in codegen mode.
The only way I've found to do this is to use a static routine in the System object which then modifies blocks in the Simulilnk model. It is very much a work around.

Akzeptierte Antwort

Salman Ahmed
Salman Ahmed am 13 Okt. 2021
Hi Jordan,
From what I understand, you want to use functions like evalin in system objects with code generation. You can define these functions as extrinsic using coder.extrinsic. The code generator does not produce code for the body of the extrinsic function and instead uses the MATLAB engine to execute the call. In other words, the code generator excludes the extrinsic function from the generated code.
Have a look at the sample code of a MATLAB System object using evalin:
classdef TimesTwo < matlab.System
%TimesTwo Multiply input by 2
% obj = TimesTwo returns a System object, obj, that
% multiples its input by two.
methods(Access = protected)
function y = stepImpl(~, u)
y=0; % Important to preallocate space for y
coder.extrinsic('evalin'); % Bypass C/C++ code generation for evalin
x = evalin('base','a'); % a=2 is defined in MATLAB workspace
y = x * u;
end
end
end
This sample is modified version of the System object used in the model of the basic example:
  2 Kommentare
Jordan McBain
Jordan McBain am 13 Okt. 2021
Is there a way of indicating that the constructor of the TimesTwo class is to be treated extrinsically? Otherwise this seems like the anser.
MATLAB documentation on coder.extrinsic states:
"The code generator does not support the use of coder.extrinsic to call local functions."
Not sure if this applies to the constructor class.
Salman Ahmed
Salman Ahmed am 14 Okt. 2021
Hi,
There are a few limitations to the use of coder.extrinsic. It is advised not to assign property values using the constructor. This practice can cause problems if you use the System object in multiple environments (such as in a System block, in a MATLAB script, and in generated code). Instead, you can write coder.extrinsic functions inside setupImpl and that will work.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Create System Objects finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by