Matlab Version

We tested the CPM Lab with Matlab 2020a. If you are using a different version, there may be differences to this documentation.

eProsima for Matlab

Although Matlab includes a DDS Support Package starting with version 2021a, it does not really fulfill our needs - its setup does not seem to be easy to automate, it only runs using Simulink and would require some work.

Instead, C++-based MEX-files are used to include DDS in our Matlab projects. These files are part of the software repository that also includes the cpm library.

All mentioned files except for are located in cpm_lib/matlab_MEX_bindings.

How to compile the MEX Files

In the following, we will assume that you already have installed FastDDS and FastCDR globally at /usr/local/lib. The script provided with our software repository takes care of that for you.

If you use our provided scripts as explained in The Easy Way, everything should work as expected. In The Hard Way, we try to explain how to do everything manually instead.

The Easy Way

Everything you need to compile the MEX files should already be provided, given that you have Matlab installed on your system and added it to your path.

  1. Just run the install script of the software repository. If Matlab is in your path, it should setup the right GCC version and replace Matlab's own GCC version. If that fails, you should see a warning regarding Matlab somewhere when running You can also directly call Make sure to call it using sudo. It needs to set up update-alternatives for GCC and also replace a file in the Matlab installation (as explained below in 'The Hard Way', which you may read if you're interested in why we have to do that).
  2. Now your system and Matlab should be set up correctly. In cpm_lib/matlab_MEX_bindings you can find a script called build_MEX.bash. This script can be used to compile all relevant MEX files. Make sure to call it using sudo. Switching the C++ compiler to a supported version, as set in the script, requires sudo privileges. The script switches back to your automatic / default compiler after compiling the MEX files. MEX files are also built using build_all.bash or the cpm lib's build.bash, unless you do not enter a sudo password when prompted to do so, or if Matlab is not installed.


The Hard Way

You need to compile these files using MEX. This should already be taken care of by the build_MEX.bash here. BUT: You need to make some preparations beforehand.

  1. Before you do so, you need to make sure that, besides Matlab, you have a supported GCC version installed on your system (see Also, please note that version 2021a of Matlab was used when developing these files, and that older versions might not be supported.

  2. Install the newest recommended gcc version (and g++ version). Then use update-alternatives to set the current system compiler to these gcc and g++ versions.

  3. Now you should be able to build the readers and writers (..._reader.cpp, ..._writer.cpp). You need to link your cpm lib file as well as the installed fastdds lib files to make the compilation work - just use the commands in build_MEX.bash as an orientation. It is expected that Matlab is installed on your system and that it has been added to your path.

    MEX vehicle_command_trajectory_writer.cpp -Lpath_to_cpm_lib_folder/build -lcpm -Ipath_to_cpm_lib_folder/include -L/usr/local/lib -lfastcdr -lfastrtps

    For the readers to work properly, they are started in a new Matlab thread. This thread seems to ignore the set C++ version in LD_PRELOAD (see How to Execute the MEX Files). The execution fails. As a result, you need to implement a workaround:

  • (Not working) Set LD_PRELOAD globally as environment variable -> Other programs may crash & this does not work
  • (Untested) Use the same "outdated" compiler for MEX and to create your library files (libcpm, libfastdds)
  • (Tested, works) Replace Matlabs internal C++ environment with a link to yours (inspired by Stackoverflow):
cd path_to_matlab/sys/os/glnxa64
sudo mkdir old
sudo mv* old
sudo ln -s /usr/lib/x86_64-linux-gnu/  


Some things need to be done before any execution of Matlab files. You can find out more about this here

  • No labels