Skip to main content

MATLAB

The ‘MATLAB’ tool offers special features to make integration into MathWorks MATLAB easier.

Requirements

The following requirements must be fulfilled to be able to use the CGI Connector with MATLAB:

MATLAB Version:
The current version is tested to run with MATLAB 2021b. However newer versions are also expected to be compatible.

MEX Compiler:
It is necessary to configure the compiler that is used by MATLAB to build a MEX File. This can be achieved by calling “mex -setup" and “mex -setup c++” from within MATLAB. The configured compiler must match with the compiler used to build the CGI Studio application.

The “mex” command must also be executable from the command line. This can be done by adding the correct path in the systems environment variables. Alternatively, the build scripts can be modified to point directly to the mex executable.

Special Features

Interfaces defined in a MATLAB module have the following tool specific properties.

drawing-7-1730341894.png
Property Name Description
Port Defines the port index of the interface for the generated MATLAB S-Function block.
When specifying ‘-1’ the port index will be calculated automatically during code generation.

Limitations

Currently the following limitation apply:

  • Interfaces that use dynamic sized data are not supported by the MATLAB tool.

Generators

This tool is located at “cgi_studio_connector/bin/MatlabCodeGenerator.exe” and generates files to be used in MathWorks MATLAB.

It has the following arguments:

Full Name Short Name Optional Description
-definitionFile -d No File containing interface definition
-complexTypes -ct Yes Path of Complex-Type definition file
-outputPath -o Yes Path for generated output files

Usage

To use the MATLAB integration the following steps are required.

  1. Build CGI Application
    As the integration into MATLAB simulation uses TCP/IP to send data between MATLAB and the CGI Application. To do this the ‘APPLICATION_IPC_ENABLED’ and ‘APPLICATION_IPC_INTERFACE_ENABLED’ CMake flags must be set. This is already done when building the application using the default builds from within the Connector.

    When running the generated MATLAB code after integration into the application code the IPC communication is however not required anymore.

  2. Build Integration Modules

    After the application is build the ‘MATLAB Integration’ can be built. The integration must be built matching Release/Debug configuration of the CGI Application. 


    If the MATLAB is already opened and the Model has been started before it is required to unload the S-Function from MATLAB before building a new version. This can be done by running ‘clear mex’ command in MATLAB.


    Note: When using the Connector, per default the CGI Application is built as Debug version.

    drawing-7-1730343117.png


  3. Startup MATLAB model
    After the build is finished the MATALAB can be opened. The default template includes all related MATLAB component in the ‘src\Matlab’ folder.
    drawing-7-1730343254.png

    The ‘CgiStudio_Startup.m’ script needs to be run in MATLAB to include the ‘CgiStudio’ code folder and files. 

    drawing-7-1730343288.png

    If already run before, it is sufficient to only run the ‘CgiStudio/Synchronize.m’ script.


  4. Add CGI Studio block
    The integration provides a ‘CGI Studio’ library which contains the ‘CGI Studio Integration’ block. 
    drawing-7-1730343343.png

    This block can be added to the Model to represent a Module defined with the CGI Connector. The provided ‘ControllerModule’ model already contains one such block per default.

  5. Configure Module
    After a ‘CGI Studio Integration’ block has been added it can be configured by double clicking it. This will open a dialog which allows the user to select the module this block should represent.
    drawing-7-1730343390.png

    After the module has been selected the block will show all configured input- and output interfaces as ports. 

    drawing-7-1730343417.png

    Note: it might be required to update the model (CTRL + D) to show the ports.

  6. Running the Simulation
    When running the simulation, the CGI Application must be started beforehand. Similar after stopping the simulation the CGI Application should be closed.

Additional Information

Actions/Events: Actions are triggered by any flank change. If an action has additional data an additional port with the suffix ‘_Data’ is generated. The same applies also to events.

Sampling Rate: To change the sampling rate of the block (by default ‘-1’ is used) the ‘CGI Studio Integration’ block itself must be opened and the parameters of the included S-Function must be configured.

drawing-7-1730343506.png

The second parameter refers to the sampling rate of the S-Function.