| Contents | Index |
| On this page… |
|---|
Considerations for Using System Objects in Generated Code Use System Objects with codegen |
You can generate C/C++ code from System objects using MATLAB Coder™ product. Using this product with System objects, you can generate efficient and compact code for deployment in desktop and embedded systems and accelerate fixed-point algorithms. System objects also support code generation using the MATLAB Function block in Simulink and the MATLAB Coder codegen function.
For general information on generating code, see
MATLAB Coder product
Simulink Coder product
Embedded Coder™ product
The following example, which uses System objects, shows the key factors to consider, such as using persistent variables, passing property values, and extrinsic functions, when you make MATLAB code suitable for code generation.
function lmssystemidentification
% LMSSYSTEMIDENTIFICATION System identification using
% LMS adaptive filter
%#codegen
% Declare System objects as persistent.
persistent hlms hfilt;
% Initialize persistent System objects only once
% Do this with 'if isempty(persistent variable).'
% This condition will be false after the first time.
if isempty(hlms)
% Create LMS adaptive filter used for system
% identification. Pass property value arguments
% as constructor arguments. Property values must
% be constants during compile time.
hlms = dsp.LMSFilter(11, 'StepSize', 0.01);
% Create system (an FIR filter) to be identified.
hfilt = dsp.DigitalFilter(...
'TransferFunction', 'FIR (all zeros)', ...
'Numerator', fir1(10, .25));
end
x = randn(1000,1); % Input signal
d = step(hfilt, x) + 0.01*randn(1000,1); % Desired signal
[~,~,w] = step(hlms, x, d); % Filter weights
% Declare functions called into MATLAB that do not generate
% code as extrinsic.
coder.extrinsic('stem');
stem([get(hfilt, 'Numerator').', w]);
end
% To compile this function use codegen lmssystemidentification.
% This produces a mex file with the same name in the current
% directory.
The following usage rules and limitations apply to using System objects in code generated from MATLAB.
Usage Rules for System Objects in Generated MATLAB Code
Assign System objects to persistent variables.
Global variables are not supported. To avoid syncing global variables between a MEX file and the workspace, use a compiler options object. For example,
f = coder.MEXConfig; f.GlobalSyncMethod='NoSync'
Then, include '-config f' in your codegen command.
Initialize System objects once by embedding the object handles in an if statement with a call to isempty( ).
Call the constructor exactly once for each System object.
Set arguments to System object constructors as compile-time constants.
Use the object constructor to set System object properties because you cannot use dot notation for code generation. You can use the get method to display properties.
Test your code in simulation before generating code.
Limitations on Using System Objects in Generated MATLAB Code
Ensure that size, type and complexity of inputs do not change.
Ensure that the value assigned to a nontunable or public property is a constant and that there is at most one assignment to that property (including the assignment in the constructor).
For most System objects predefined in the software, the only time you can set their properties during code generation is when you construct the objects. System objects that support tunable properties at any time during code generation are listed in the product's code generation support table. For System objects that you define, you can also change their tunable properties at any time during code generation.
Do not change the size of properties during code generation.
The only System object methods supported in code generation are
get
getNumInputs
getNumOutputs
isDone (for sources only)
reset
step
Do not set System objects to become outputs from the MATLAB Function block.
Do not pass a System object as an example input argument to a function being compiled with codegen.
Do not pass a System object to functions declared as extrinsic (i.e., functions called in interpreted mode) using the coder.extrinsic function. Do not return System objects from any extrinsic functions.
You can include System objects in MATLAB code in the same way you include any other elements. You can then compile a MEX file from your MATLAB code by using the codegen command, which is available if you have a MATLAB Coder license. This compilation process, which involves a number of optimizations, is useful for accelerating simulations. See the MATLAB Coder User's Guide and Code Generation for MATLAB Classes for more information.
Using the MATLAB Function block, you can include a MATLAB language function in a Simulink model. This model can then generate embeddable code. You can include any System object in the MATLAB Function block. System objects provide higher-level algorithms for code generation than do most associated blocks. For more information, see Introduction to MATLAB Function Blocks in the Simulink documentation.
Note MATLAB Compiler software supports System objects for use inside MATLAB functions. The compiler product does not support System objects for use in MATLAB scripts. |
![]() | Find Help and Demos for System Objects |

Learn how to apply early verification to your development process through these technical resources.
How much time do you spend on testing to ensure implementation meets system-level requirements?
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |