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 aMATLAB Functionblock.MATLAB Functionblocks call C code using MATLAB®commands. You can also generate code from models withMATLAB Functionblocks that call external C code.

Call C Code from a金宝appModel

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 aMATLAB Functionblock into your model.

  3. In theMATLAB Functionblock, use thecoder.cevalfunction to call the C code. To pass data by reference, usecoder.ref,coder.rref, orcoder.wref.

  4. Specify the C source and header files in theSimulation Targetpane 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 theSimulation Targetpane, in theInclude Directoriestext box.

    Alternatively, use thecoder.cincludeandcoder.updateBuildInfofunctions to specify source and header files in your MATLAB code. To develop an interface to external code, you can use thecoder.ExternalDependencyclass. To see which workflow is supported, seeImport custom code.

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

  6. If you have aSimulink Coder™license, you can generate code for targets. To use the same source and header files for code generation, open Configuration Parameters, navigate to theCode Generation>Custom Codepane, and enableUse 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 thecoder.targetfunction.

Usecoder.cevalin an ExampleMATLAB FunctionBlock

This example shows how to call the simple C programdoubleItfrom aMATLAB Functionblock.

  1. Create the source filedoubleIt.cin your current working folder.

    #include "doubleIt.h" double doubleIt(double u) { return(u*2.0); }
  2. Create the header filedoubleIt.hin your current working folder.

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

  4. 在图书馆浏览器,导航到金宝app>User-Defined Functionslibrary, and add aMATLAB Functionblock to the model.

  5. Double-click the block to open theMATLAB Function Block Editor. Enter code that calls thedoubleItprogram:

    functiony = callingDoubleIt(u) y = 0.0; y = coder.ceval('doubleIt',u);
  6. Connect aConstantblock that has a value of3.5to the input port of theMATLAB Functionblock.

  7. Connect aDisplayblock to the output port.

    这张图片显示了一个MATLAB功能块设置为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 theSimulation Targetpane.

  9. In theInsert custom C code in generatedsection, selectHeader fileand enter#include "doubleIt.h".

  10. In theAdditional build informationsection, selectSource files, enterdoubleIt.c, and clickOK.

    This image shows the Simulation Target pane. The text #include

  11. Run the simulation. The value7appears in theDisplayblock.

Control Imported Bus and Enumeration Type Definitions

When you call external C code by usingMATLAB Functionblocks or Stateflow®, 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 theGenerate typedefs for imported bus and enumeration typesparameter. To find this parameter, open the Configuration Parameters window, navigate to theSimulation Targetpane, and expand theAdvanced parameterssection.

To configure Simulink to automatically generate type definitions, enableGenerate typedefs for imported bus and enumeration types. To include a custom header file that defines the enumeration and bus types, clearGenerate typedefs for imported bus and enumeration typesand list the header file in theHeader filetext box.

See Also

||||||||

Related Examples

More About