Generate Code for MATLAB Functions That Use System Objects
You can use System objects in MATLAB code intended for code generation. A System object™ is a type of handle class that derives from the matlab.System class. For more information about System objects in MATLAB, see What Are System Objects?.
This example shows how to use the codegen command to generate standalone C code for a user-defined System object. When using System objects in MATLAB code for code generation, certain considerations apply. See Class Limitations for Code Generation and System Objects in MATLAB Code Generation.
Examine System Object and MATLAB Function
Examine the System object AddOne. This System object increments an input value by one. The System object step method calls the AddOne method stepImpl.
type AddOne.mclassdef AddOne < matlab.System
methods (Access=protected)
function y = stepImpl(~,x)
y = x+1;
end
end
end
Examine the MATLAB function testAddOne, which uses the AddOne System object.
type testAddOne.mfunction y = testAddOne(x) %#codegen p = AddOne(); y = p.step(x); end
Test the MATLAB function by using a sample input value.
testAddOne(6)
ans = 7
Generate and Run MEX Function
Generate a MEX function for the testAddOne function by using the codegen command. Then, run the generated MEX function to check that the generated code has the same behavior as the original MATLAB code.
It is a best practice to perform this step because you can run the generated MEX function to detect run-time errors that are harder to diagnose in standalone code. For example, the MEX function includes memory integrity checks by default.
By default, the codegen command generates a MEX function in C in the working folder. Use the -args option with an example scalar value to specify that the input argument to testAddOne is a scalar double.
codegen testAddOne -args {0}
Code generation successful.
Test the MEX function with the same input that you passed to the original MATLAB function. The MEX function produces the same output.
testAddOne_mex(6)
ans = 7
Generate C Static Library
Generate a C static library by using the codegen command with the -config:lib option. To prevent the code generator from inlining the stepImpl method, disable inlining by using the -O disable:inline option. Use the same -args syntax that you used to generate the MEX function.
codegen -config:lib -O disable:inline testAddOne -args {0}
Warning: Code generation is using a coder.EmbeddedCodeConfig object. Because Embedded Coder is not installed, this might cause some Embedded Coder features to fail. Code generation successful (with warnings): View report
Examine the generated code. The C function testAddOne calls the C function SystemCore_step, which calls the function AddOne_stepImpl.
file = fullfile("codegen","lib","testAddOne","testAddOne.c"); coder.example.extractLines(file,"/* Function Definitions */","}",1,1)
/* Function Definitions */
/*
* Arguments : double x
* Return Type : double
*/
double testAddOne(double x)
{
return SystemCore_step(x);
}
file = fullfile("codegen","lib","testAddOne","SystemCore.c"); coder.example.extractLines(file,"/* Function Definitions */","}",1,1)
/* Function Definitions */
/*
* Arguments : double varargin_1
* Return Type : double
*/
double SystemCore_step(double varargin_1)
{
return AddOne_stepImpl(varargin_1);
}
file = fullfile("codegen","lib","testAddOne","AddOne.c"); coder.example.extractLines(file,"/* Function Definitions */","}",1,1)
/* Function Definitions */
/*
* Arguments : double x
* Return Type : double
*/
double AddOne_stepImpl(double x)
{
return x + 1.0;
}
See Also
codegen | handle | matlab.System