Knowledge Base /
Technical Notes
Integrating MATLAB Models with Mimic Using MEMI
By Adam Drees
Product: Mimic Simulation

The purpose of this technical note is to demonstrate how to use a model developed in MathWorks MATLAB with Mimic using the Mimic External Modeling Interface (MEMI).

Using the MATLAB Compiler, it is possible to create a shared library from MATLAB code written as an m-file, known as a Dynamic Link Library, which can be run outside of MATLAB. The current MEMI version allows DLLs to run in Mimic. Since variables in MATLAB are treated as matrices, it would be impossible to directly compile MATLAB code into something that is useable by MEMI but it is possible to create a DLL wrapper that executes the MATLAB shared library.

Building the MATLAB Shared Library

A simple MATLAB function was created to demonstrate compiling MATLAB functions into a shared library.

This function will output a single X position based on the inlet variables. The arguments are separated as Adoubles and Bints because matrices in MATLAB can only handle one data type.

The MATLAB function is compiled by typing in the MATLAB command window:

Executing the command will generate several important files:

  • myMATLABcompiled.lib & myMATLABcompiled.h are necessary for compiling your C++ wrapper DLL.
  • myMATLABcompiled.dll will be executed by your C++ wrapper DLL.
  • readme.txt provides further details on using the MATLAB DLL such as running it on another machine.

Building the Wrapper Shared Library

Since MEMI 2 can not directly run the MATLAB DLL, an addition shared library will need to be created to coordinate between MEMI2 and the MATLAB DLL.

The C++ code created for the MATLAB DLL wrapper has a few essential parts. In the InitializeMEMI function, the MATLAB runtime application and the MATLAB library need to be initialized.

During the ExecuteMEMI function, the MATLAB function arguments must be declared as mwArray variables. Searching the internet for mwArray will provide more information for handling the special MATLAB data types.

After data is loaded into the mwArray varibles, the MATLAB function can finally be executed.

In the CleanUpMEMI function, close the library and application

To compile it, it must be placed in the MATLAB current folder. The command to compile it is

Compiling it will generate the file MATLABwrapper.dll

Configuring Mimic

Begin by copying both .dll files (myMATLABcompiled.dll & MATLABwrapper.dll) into the mimic folder (C:\Program Files\MYNAH Technologies\Mimic). This is necessary for the myMATLABcompiledInitialize() function to execute.

After they are in place, open Mimic Explorer as administrator and navigate the External Models section in your chosen node. Create a new Definition, and under it, create a new model. Right click on the definition and select properties. In the section labeled “DLL FILE PATH:” select the ellipsis and navigate to your wrapper DLL. A popup should appear that prompts the user to replace the Existing Variables.

If MEMI2 provides you with a pop-up error, you can try debugging the issue by running the following commands as a batch file:

Using Mimic the MATLAB DLL on a Separate Machine

To run the MATLAB DLL on a separate machine, you will need to install the MATLAB runtime. Install the MATLAB runtime by following the directions in readme.txt, which is found by compiling the MATLAB code.

64-BIT/32-BIT Limitation

At the time of writing this, MATLAB is only sold as a 64-bit version, but Mimic is available in 32-bit and 64-bit versions. MATLAB normally only compiles as 64-bit code and therefore can only create DLL that works with 64-bit Mimic.

Software Requirements

The MEMI functionality used to integrate Matlab models is available in Mimic v3.6.2 or later.

According to the MathWorks website, the the necessary components for Matlab would be:

  • MATLAB License version R2016a
  • MATLAB Compiler
  • MATLAB Compiler SDK

MYNAH Technologies can provide model integration services and DLL development to aid in the Matlab or other external model integration to Mimic.

For any questions regarding this knowledge base article, contact MYNAH at support@mynah.com or +1.636.728.2000.