Main Content

Integrate C Code by Using the MATLAB Function Block

If you have C code that you want to use in your Simulink® model, you can call external C code to the model using a MATLAB Function block. MATLAB Function blocks call C code using MATLAB® commands. You can also generate code from models with MATLAB Function blocks that call external C code.

Call C Code from a Simulink Model

To call external C code in a Simulink model, follow these steps:

  1. Identify the source (.c) and header (.h) files that contain the C code you want to use in your model.

  2. Insert a MATLAB Function block into your model.

  3. In the MATLAB Function block, use the coder.ceval function to call the C code. To pass data by reference, use coder.ref, coder.rref, or coder.wref.

  4. Specify the C source and header files in the Simulation Target pane of the Configuration Parameters window. Include the header file using double quotations, for example, #include "program.h". If you need to access C source and header files outside your working folder, list the path in the Simulation Target pane, in the Include Directories text box.

    Alternatively, use the coder.cinclude and coder.updateBuildInfo functions to specify source and header files in your MATLAB code. To develop an interface to external code, you can use the coder.ExternalDependency class. To see which workflow is supported, see Import custom code.

  5. Test your Simulink model and ensure it functions correctly.

  6. If you have a Simulink Coder™ license, you can generate code for targets. To use the same source and header files for code generation, open Configuration Parameters, navigate to the Code Generation > Custom Code pane, and enable Use the same custom code settings as Simulation Target. You can also specify different source and header files.

    To conditionalize your code to execute different commands for simulation and code generation, you can use the function.

Use coder.ceval in an Example MATLAB Function Block

This example shows how to call the simple C program doubleIt from a MATLAB Function block.

  1. Create the source file doubleIt.c in your current working folder.

    #include "doubleIt.h"
    double doubleIt(double u)
  2. Create the header file doubleIt.h in your current working folder.

    #ifndef MYFN
    #define MYFN
    double doubleIt(double u);
  3. Create a new Simulink model. Save it as myModel.

  4. In the Library Browser, navigate to the Simulink > User-Defined Functions library, and add a MATLAB Function block to the model.

  5. Double-click the block to open the MATLAB Function Block Editor. Enter code that calls the doubleIt program:

    function y = callingDoubleIt(u)
    y = 0.0;
    y = coder.ceval("doubleIt",u);
  6. Connect a Constant block that has a value of 3.5 to the input port of the MATLAB Function block.

  7. Connect a Display block to the output port.

    This image shows a MATLAB Function block set to output to a Display block. The MATLAB Function uses a constant block with a value of 3.5 as an input.

  8. Open the Configuration Parameters window, and navigate to the Simulation Target pane.

  9. In the Code information tab, click Include headers, then enter #include "doubleIt.h".

    This image shows the Simulation Target pane. The text #include "doubleIt.h" has been included in the Include headers section.

  10. Click the Source files tab and enter doubleIt.c. Click OK.

    This image shows the Simulation Target pane. The text #include doubleIt.c has been included in the Source file section.

  11. Run the simulation. The value 7 appears in the Display block.

Control Imported Bus and Enumeration Type Definitions

When you call external C code by using MATLAB Function blocks, you can control the type definitions for imported buses and enumerations in your model. Simulink can generate type definitions for you, or you can supply a header file containing the type definitions. You can control this behavior by toggling the Generate typedefs for imported bus and enumeration types parameter. To find this parameter, open the Configuration Parameters window, navigate to the Simulation Target pane, and expand the Advanced parameters section.

To configure Simulink to automatically generate type definitions, enable Generate typedefs for imported bus and enumeration types. To include a custom header file that defines the enumeration and bus types, clear Generate typedefs for imported bus and enumeration types and list the header file in the Header file text box.

See Also



Related Topics