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.
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:
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
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:
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.
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.
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:
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 firstname.lastname@example.org or +1.636.728.2000.
MYNAH Technologies LLC
390 South Woods Mill Road, Suite 100
Chesterfield, MO 63017 USA
© MYNAH Technologies 2012 - 2019. All rights reserved.
Designs are marks of MYNAH Technologies, Emerson Process Management, DeltaV, and the DeltaV design are marks of one of the Emerson Process Management of companies. All other marks are property of their respective owners. The contents of this publication are presented for informational purposes only, and while every effort has been made to ensure their accuracy, they are not to be construed as warrantees or guarantees, expressed or implied, regarding the products or services described herein or their use or applicability. All sales are governed by our terms and conditions, which are available on request. We reserve the right to modify or improve the design or specification of such products at any time without notice.
While this information is presented in good faith and believed to be accurate, Mynah Technologies does not guarantee satisfactory results from reliance upon such information. Nothing contained herein is to be construed as a warranty or guarantee, express or implied, regarding the performance, merchantability, fitness or any other matter with respect to the products, nor as a recommendation to use any product or process in conflict with any patent. Mynah Technologies reserves the right, without notice, to alter or improve the designs or specifications of the products described herein.