Skip to Main Content Skip to Search
Product Documentation

Use System Objects for Code Generation from MATLAB

Considerations for Using System Objects in Generated Code

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

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

Limitations on Using System Objects in Generated MATLAB Code

Use System Objects with codegen

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.

Use System Objects with the MATLAB Function Block

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.

Use System Objects with MATLAB Compiler

  


Free Early Verification Kit

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