Main Content

Implement Algorithm that Calls External C Code


This example shows how to use external C code in a System object.

System objects

System objects allow you to implement algorithms using MATLAB. System objects are a specialized kind of MATLAB object, designed specifically for implementing and simulating dynamic systems with inputs that change over time.

After you define a System object, you can include it in a Simulink model using a MATLAB System block.

Model Description

The MATLAB System block uses the System object ExternalCCode that calls external C function extSum to compute the sum of its input elements. The Display block shows the result of the sum of values from the source block.

System Object Class Definition

You can access MATLAB source code used by the MATLAB System block by clicking the "Source code" hyperlink from the block dialog. The System object ExternalCCode implements the stepImpl method to compute its output. stepImpl calls external C function extSum to do the computation. The System object inherits from the coder.ExternalDependency class and implements the following methods to use external C code.

  • getDescriptiveName - Return the name you want to associate with external dependency

  • isSupportedContext - Return true if external dependency is supported in the current build context

  • updateBuildInfo - Provide additional information required to link external code

The System object also calls coder.cinclude from stepImpl method to include external C header file sum.h.

classdef ExternalCCode < matlab.System & coder.ExternalDependency
% ExternalCCode Compute output by calling into external C Code
    methods(Access = protected)
        function y = stepImpl(~, a)
            % Add header file sum.h to build
            y = 0.0; %#ok<NASGU> % Pre-initialize y since coder cannot
                                 % identify the type of output y from an
                                 % external C function
            % Call external C function to calculate sum of input elements
            y = coder.ceval('extSum', coder.rref(a), int32(numel(a)));

        function bName = getDescriptiveName(~)
        % Return a descriptive name for the external dependency. Code
        % generator uses this name for error messages.
            bName = 'SumAPI';

        function tf = isSupportedContext(~)
        % Use this function to determine whether current build context
        % supports external dependency. Build context includes information
        % about target language and code generation target.
            tf = true;

        function updateBuildInfo(buildInfo, ~)
            % Add source file sum.c to build

External C code

External C function extSum is defined in sum.c file.

#include "sum.h"

double extSum(const double *a, int numElems)
    int ii;
    double sum = 0.0;
    for (ii=0; ii < numElems; ii++) {
        sum += a[ii];
    return sum;

extSum is declared in sum.h.

#ifndef SUM_H
#define SUM_H

extern double extSum(const double *a, int numElems);


See Also

| |

Related Topics