Best way to wrap C++ library to be used in MATLAB, Simulink, and others?

8 views (last 30 days)
We are developing a versatile library of models for simulating major portions of our product. To avoid duplication of work, we're developing the models in C++, and intending to make them usable from MATLAB, Simulink, and Python, at the very least. The library will be built to a DLL by our CI build. I would then like CI to build appropriate files for utilizing the code in the DLL from MATLAB and Simulink.
I have very little experience with MATLAB and Simulink. I've read some of the documentation on S-Functions and related topics so I have some understanding of what that is. It looks like we could wrap our DLL with S-Functions for Simulink. But I'm not sure if that's exactly the right approach or not. I'd like to be able to give the engineers using Simulink a very simple way to install this library and have the blocks show up for them to use in Simulink.
Questions:
  • How do we provide a library of blocks for engineers to use in Simulink? How would that be packaged up? Is it possible to do this in our CI build or does it need to be done within Simulink?
  • How does Simulink indicate the passage of time? Right now the C++ API has a method Step(int milliseconds) on each model so we can simulate the specified amount of time passing. Does Simulink do something like this? We'll want to simulate faster than real-time (I think "Accelerated" is what Simulink calls this?), so we need to know how much simulated time has passed between each call.
  • For MATLAB, how do we provide a library of classes for engineers to use? Ideally they should be able to drop in a set of files and then be able to instantiate instances of our classes and run their functions.
  • What kinds of things should we avoid in our C++ library that might break either Simulink or MATLAB? Can we use GPU code (CUDA)? GUIs? Disk I/O?
Thanks for your time!

Accepted Answer

Mark McBroom
Mark McBroom on 7 May 2023
For both Simulink and MATLAB, it is easiest to first compile your C++ code into a library, then use the following approaches for creating class instances and calling class methods.
  1. To create instances of C++ classes and call C++ methods: https://www.mathworks.com/help/simulink/slref/cfunction.html#
  2. There are a couple of approaches for specifying execution rates in Simulink. The most common methods are to set the sample time of an input port or the sample time of a subsystem. See this link for details; https://www.mathworks.com/help/simulink/ug/how-to-specify-the-sample-time.html# The Simuilnk model will run as fast as your host computer will allow. Larger/more complex Simulink models will run slower than simple models. Accelerated mode generates C code for the entire Simulnk model allowing it to run faster than the same model in Normal mode.
  3. Use clib to access the C++ code from MATLAB: https://www.mathworks.com/help/matlab/matlab_external/call-functions-in-cpp-shared-library.html
  4. Disk I/O and CUDA code should be fine.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!