You can call external C code from a Simulink® model using a MATLAB Function block. Follow these high-level steps:
Start with existing C code consisting of the source (.c
) and
header (.h
) files.
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
.
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.
Test your Simulink model and ensure it functions correctly.
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.
coder.ceval
in a MATLAB Function BlockThis example shows how to call the simple C program doubleIt
from a
MATLAB Function block.
Create the source file doubleIt.c
in your current working
folder.
#include "doubleIt.h" double doubleIt(double u) { return(u*2.0); }
Create the header file doubleIt.h
in your current working
folder.
#ifndef MYFN #define MYFN double doubleIt(double u); #endif
Create a new Simulink model. Save it as myModel
.
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.
Enter code that calls the doubleIt
program:
function y = callingDoubleIt(u) y = 0.0; y = coder.ceval('doubleIt',u);
Connect a Constant block having a value of 3.5
to the input port
of the MATLAB Function block.
Connect a Display block to the output port.
In the Model Configuration Parameters dialog box, open the Simulation Target pane.
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.
In the Additional build information section, select
Source files from the list, enter doubleIt.c
in the Source files text box, and click
OK.
Run the simulation. The value 7
appears in the Display
block.
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:
Clear the Generate typedefs for imported bus and enumeration types check box.
List the header file in the Simulation Target pane, in the Header file text box.
To configure Simulink to automatically generate type definitions:
Select the Generate typedefs for imported bus and enumeration types check box.
Do not list a header file that corresponds to the buses or enumerations.
coder.BuildConfig
| coder.ceval
| coder.cinclude
| coder.ExternalDependency
| coder.ref
| coder.rref
| coder.target
| coder.updateBuildInfo
| coder.wref