This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Integrate C Code Using the MATLAB Function Block

Call C Code from a Simulink Model

You can call external C code from a Simulink® model using a MATLAB Function block. Follow these high-level steps:

  1. Start with existing C code consisting of the source (.c) and header (.h) files.

  2. In the MATLAB Function block, enter the MATLAB® code that calls the C code. Use the coder.ceval function. To pass data by reference, use coder.ref, coder.rref, or coder.wref.

  3. Specify the C source and header files for simulation in the Simulation Target pane of the Model Configuration Parameters dialog box. 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 within 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.

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

  5. If you have a Simulink Coder™ license, you can generate code for targets using this method. To use the same source and header files for code generation, click Use the same custom code settings as Simulation Target in the Code Generation > Custom Code pane. 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 coder.target function.

Use coder.ceval in a 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)
    {
         return(u*2.0);
    }
    
  2. Create the header file doubleIt.h in your current working folder.

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

  4. In the Library Browser, from User-Defined Functions, add a MATLAB Function block to the model and double-click the block to open the editor.

  5. Enter code that calls the doubleIt program:

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

  7. Connect a Display block to the output port.

  8. In the Model Configuration Parameters dialog box, open the Simulation Target pane.

  9. In the Insert custom C code in generated section, select Header file from the list, and enter #include "doubleIt.h" in the Header file text box.

  10. In the Additional build information section, select Source files from the list, enter doubleIt.c in the Source files text box, and click OK.

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

Control Imported Bus and Enumeration Type Definitions

This procedure applies to simulation only.

Simulink generates code for MATLAB Function blocks and Stateflow® to simulate the model. When you call external C code using MATLAB Function blocks or Stateflow, you can control the type definitions for imported buses and enumerations in model simulation.

Simulink can generate type definitions, or you can supply a header file containing the type definitions. You control this behavior using the Generate typedefs for imported bus and enumeration types check box in the Model Configuration Parameters dialog box.

To include a custom header file defining the enumeration and bus types:

  1. Clear the Generate typedefs for imported bus and enumeration types check box.

  2. List the header file in the Simulation Target pane, in the Header file text box.

To configure Simulink to automatically generate type definitions:

  1. Select the Generate typedefs for imported bus and enumeration types check box.

  2. Do not list a header file that corresponds to the buses or enumerations.

See Also

| | | | | | | |

Related Examples

More About