Main Content

Code Generation with VideoDevice System Object

Using the codegen Function

The VideoDevice System object™ supports code generation in MATLAB® via the codegen function. To use the codegen function, you must have a MATLAB Coder™ license. System objects also support code generation using the MATLAB Function block in Simulink®. You can also use the System object with MATLAB Compiler™.


The MATLAB Compiler software supports System objects for use inside MATLAB functions. The MATLAB Compiler does not support System objects for use in MATLAB scripts.


If you use the codegen command to generate a MEX function on a Windows® platform, you need to perform imaqreset before running the generated MEX file. However, MEX does not work correctly if the Kinect® for Windows Sensor support package is installed.

After running the generated MEX file, if you run some MATLAB code that includes a VideoDevice System object with a camera adaptor that is also used in the generated MEX file, you need to perform clear mex first.


The codegen command can be used to generate executable files on non-Windows platforms. However, generation of the MEX function is not supported on Linux® and macOS platforms.

For more information see the documentation for the MATLAB codegen function.

Shared Library Dependencies

The VideoDevice System object generates code with limited portability. The System object uses precompiled shared libraries, such as DLLs, to support I/O for specific types of devices. The shared library locations that the generated executable requires are as follows:

  • Specific MathWorks shared libraries under [MATLABROOT]\bin\<ARCH>\

  • MathWorks adaptor libraries under [MATLABROOT]\SupportPackages\<RELEASE>\toolbox\imaq\supportpackages\genericvideo\adaptor\<ARCH>\ specific to the device selected.

For example, your path may look like this on a Windows system and using release R2018a:


You will need to add the above folders to your system path before running the generated executable outside of MATLAB.

Usage Rules for System Objects in Generated MATLAB Code

  • Assign System objects to persistent variables.

  • Global variables are not supported.

  • Initialize System objects once by embedding the object handles in an if statement with a call to isempty( ).

  • Call the constructor exactly once for each System object.

  • Set arguments to System object constructors as compile-time constants.

  • Use the object constructor to set System object properties because you cannot use dot notation for code generation. You can use the get function to display properties.

  • Test your code in simulation before generating code.

The following shows an example of some of these rules.

% Note: System Objects created for Codegen have to be persistent variables.
persistent vid;

% Construct the IMAQ VideoDevice System Object.
if isempty(vid)
    % Note: All required parameters must be passed to the System Object at
    % the point of construction.
    vid = imaq.VideoDevice('winvideo', 1, 'MJPG_320x240', ...
                           'ROI', [1 1 320 240], ...
                           'ReturnedColorSpace', 'rgb', ...
                           'DeviceProperties.Brightness', 130, ...
                           'DeviceProperties.Sharpness', 220);

Limitations on Using System Objects in Generated MATLAB Code

Ensure that the value assigned to a nontunable or public property is a constant and that there is at most one assignment to that property (including the assignment in the constructor). Do not set any properties during code generation.

The only System object functions supported in code generation are:

  • get

  • getNumInputs

  • getNumOutputs

  • reset

  • step

Do not set System objects to become outputs from the MATLAB Function block.

Do not pass a System object as an example input argument to a function being compiled with codegen.

Do not pass a System object to functions declared as extrinsic (i.e., functions called in interpreted mode) using the coder.extrinsic function. Do not return System objects from any extrinsic functions.