A possible way to work with Simulink is to parse the persisted model files. As a well known major problem with this approach is that the format of the serialized models might change over time (e.g. in 2012 suddenly the .slx format became default instead of the .mdl), causing extensive additional work to adapt to these changes for tools based on parsing serialized MATLAB file formats. For this reason, Massif uses the Java interface of a running MATLAB and discovers the model using Simulink model specific MATLAB commands. This way it is independent from the format of the files in which models are stored. However, it requires at least one running MATLAB instance to work. Currently, three MATLAB Connectors are supported:

  • MatlabEngine: this is the official Java API provided for MATLAB. Massif can connect to an already running MATLAB instance if matlab.engine.shareEngine is issued.

  • Command Evaluation Server (in CE Server): it runs within the running MATLAB instance itself. To support this version, an additional server component (called MatlabServer) is needed to be downloaded from separate links provided for each version in the overview (details on how to set it up is discussed later).

  • MatlabControl: this one is based on the open source MatlabControl package. Currently, it starts one MATLAB instance and doesn’t close it.

MatlabEngine

Recent versions of MATLAB come with an official Java API. Starting from Massif version 0.7.0, you can use this as the connector for MATLAB.

To do so, you need to

  1. Copy the engine.jar from $MATLAB_ROOT/extern/engines/java/jar to $JAVA_HOME/jre/lib/ext.

  2. Set the -Dorg.osgi.framework.bootdelegation=com.mathworks.* and -Dosgi.parentClassloader=ext Java VM arguments when starting Massif to prevent issues caused by different classloaders.

  3. Provide the -Djava.library.path=$MATLAB_ROOT/bin/glnxa64/ Java VM argument to let Massif find the libnativemvm.so dynamic library required by the engine.

  4. Start MATLAB and execute the following commands:

    • addpath(%MASSIF_LOCATION%\git\massif\plugins\hu.bme.mit.massif.simulink.api\scripts') to add MATLAB scripts used by Massif to your MATLAB workspace.

    • matlab.engine.shareEngine('Engine_1') to share the MATLAB Engine which will be used by Massif to communicate.

If you would like to use the matlabengine connector from plain java application, please use the massif/maven/massif-matlabengine/. In this case, MATLABROOT must be set as an environmental variable pointing to your MATLAB root folder containing the /bin folder (for example: C:\Program Files\MATLAB\R2018b).

Command Evaluation Server (MatlabServer)

Another option is to use an RMI client-server based solution to execute MATLAB commands programmatically. This option can be selected under the preference page shown in the figure below.

Command Evaluation Server

This allows connection to an already running MATLAB session. In order to do so, a MatlabServer has to be started from within MATLAB. After you downloaded the zip containing the MatlabServer, extract it to a folder on your computer, then make that folder as your current folder in MATLAB.

Edit the Java properties file named matlabserverconfig.properties with the serveraddress and serverport parameters. For example, set serveraddress = 127.0.0.1 and serverport = 1098. Run the setup_remote script. On the screen it will print the registered service name, which has to be entered on the preference page in Eclipse. If everything works well, you should get an output similar to the following:

>> setup_remote
Configuration path not provided, considering the following path: PATH\matlabserverconfig.properties

Host address = 127.0.0.1
Host port: 1098
Debug mode: null
Starting MATLAB RMI Server...

Address = 127.0.0.1
port = 1098
serviceName = MATLABModelProviderr2012a480

MATLAB RMI Server Started!

Copy the service name into the 'Service name' field in the Simulink Preferences window. Execute addpath(%MASSIF_LOCATION%\git\massif\plugins\hu.bme.mit.massif.simulink.api\scripts') command in MATLAB to add scripts used by Massif to your MATLAB workspace.

After this setup, the importer is ready to use. Please note, that the Command Evaluation Server-based approach is currently only available for Windows.

Enable debug mode

Open the matlabserverconfig.properties file (next to the setup-remote script) and uncomment the debug = true line. If this property is set, the MATLAB console will print each executed command and you may be able to solve some issues that are hard to debug on the client side.

MatlabControl

Another option is to use the open source MatlabControl Java API for communicating with MATLAB. This is integrated to Massif, the user can select this in Eclipse under the Window/PreferencesSimulink preferences page.

MatlabControl

This connector handles MATLAB sessions itself. Currently it operates the following way: opens a new MATLAB instance at the first call and executes all the required commands in this instance.
To use this connector you also have to execute the addpath(%MASSIF_LOCATION%\git\massif\plugins\hu.bme.mit.massif.simulink.api\scripts') command to add MATLAB scripts used by Massif to your MATLAB workspace.
Connection or reconnection to a running MATLAB session is not supported with this connector.

Important: If you use additional libraries in Simulink, you will have to make sure the massif_functions.m script is on the MATLAB search path when you are running the import (see issue #80 for details).