Generate Code for MATLAB Functions That Use Singleton Classes
You can use a singleton class in MATLAB® code intended for code generation. A singleton class is a type of handle class that you can instantiate only once.
To create a singleton class:
Define a private constructor.
Define a persistent variable in a static method.
Create an instance of the singleton class and assign it to the persistent variable.
Enclose the persistent variable assignment in an
ifstatement with theisemptyfunction to make sure that the persistent variable is unused.
This example shows how to create a singleton class and how to use the codegen command to generate code for the class. When using handle classes in MATLAB code for code generation, certain considerations apply. See Class Limitations for Code Generation.
Examine the MATLAB handle class MySingletonClass. This class has a single method, increment, which increments the property Value.
type MySingletonClass.mclassdef MySingletonClass < handle
properties
Value = 0
end
methods (Access=private)
function obj = MySingletonClass
end
end
methods (Static)
function obj = instance
persistent p
if isempty(p)
p = MySingletonClass;
end
obj = p;
end
end
methods
function increment(obj)
obj.Value = obj.Value + 1;
end
end
end
Examine the function useMyClass, which creates an instance of the singleton class and uses the increment method to increment the Value property.
type useMyClass.mfunction out = useMyClass %#codegen myObj = MySingletonClass.instance; myObj.increment; out = myObj.Value; end
Test the MATLAB function. Clear the singleton object before running the MATLAB function.
clear MySingletonClass
useMyClassans = 1
useMyClass
ans = 2
Generate and Run MEX Function
Generate a MEX function for the useMyClass 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.
codegen useMyClassCode generation successful.
Test the MEX function. Reset the singleton object by clearing the MEX function before running it. The MEX function produces the same output as the MATLAB function.
clear useMyClass_mex
useMyClass_mexans = 1
useMyClass_mex
ans = 2
Generate and Inspect C Code
Generate a C static library by using the codegen command with the -config:lib option. To prevent the code generator from inlining the increment method, use the -O disable:inline option.
codegen -config:lib -lang:c -O disable:inline useMyClass
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 definition of the C singleton class MySingletonClass in the file MySingletonClass.c. The function MySingletonClass_instance defines the singleton value, and the function c_MySingletonClass_MySingletonC initializes it.
file = fullfile("codegen","lib","useMyClass","MySingletonClass.c"); coder.example.extractLines(file,"/* Function Definitions */","/* End of code generation (MySingletonClass.c) */",1,0)
/* Function Definitions */
/*
* Arguments : MySingletonClass *obj
* Return Type : void
*/
static void c_MySingletonClass_MySingletonC(MySingletonClass *obj)
{
obj->Value = 0.0;
}
/*
* Arguments : MySingletonClass *obj
* Return Type : void
*/
void MySingletonClass_increment(MySingletonClass *obj)
{
obj->Value++;
}
/*
* Arguments : void
* Return Type : MySingletonClass *
*/
MySingletonClass *MySingletonClass_instance(void)
{
static MySingletonClass p;
if (!p_not_empty) {
c_MySingletonClass_MySingletonC(&p);
p_not_empty = true;
}
return &p;
}
/*
* Arguments : void
* Return Type : void
*/
void MySingletonClass_instance_init(void)
{
p_not_empty = false;
}
/*
* File trailer for MySingletonClass.c
*
* [EOF]
*/
See Also
codegen | handle | matlab.System