Full Library / System_Language / MatLab / Interface_to_MatLab
Code File Location: VisualSim/matlab/Expression
The VisualSim Matlab interfaces with the Matlab engine, evaluate Matlab expressions and return the results to VisualSim. The Matlab interface has been fully tested under Windows, OSX and Linux. The MatLab install must be mounted on the local machine for this interface to work correctly. There are a sequence of settings that must be completed before the interface can be first invoked. The details are here.
On each firing send an expression for evaluation to a matlab Engine. The expression is any valid matlab expression, e.g.:
[out1, out2, ... ] = SomeMatlabFunctionOrExpression( in1, in2, ... );...
The expression may include references to the input port names, current time (time), and a count of the firing (iteration). This is similar to Processing block. To refer to parameters , use $parameter_name or ${parameter_name} within the expression. All output ports must be on the LHS of the MatLab expression. To use the fields of an incoming data structure, the format is portname.fieldname.
The Matlab engine is opened (started) during prefire() by the first matlab Expression actor. Subsequent open()s simply increment a use count.
At the start of fire(), clear variables;clear globals commands are sent to matlab to clear its workspace. This helps detect errors where the matlab expression refers to a Matlab variable not initialized from the input ports of this actor instance.
After the evaluation of the matlab expression is complete, the fire() method iterates through names of output ports and converts matlab variables with corresponding names to Tokens that are sent to the corresponding output ports. Incorrect expressions are usually first detected at this point by not finding the expected variables. If an output port variable is not found in the matlab Engine, an exception is thrown. The exception description string contains the last stdout of the Matlab engine that usually describes the error.
The get1x1asScalars and getIntegerMatrices control data conversion (see Engine and Engine.ConversionParameters).
A Parameter named packageDirectories may be added to this actor to augment the search path of the matlab engine during the firing of this actor. The value of this parameter should evaluate to a StringToken, e.g.:
"path1, path2, ..."
containing a comma-separated list of paths to be prepended to the matlab engine search path before expression is evaluated. The list may contain paths relative to the directory in which VisualSim was started, or any directory listed in the current classpath (in that order, first match wins). See regEx function called findFile(String).
After evaluation, the previous search path is restored.
A Parameter named _debugging may be used to turn on debug print statements to stdout from Engine and the interface. An IntToken with a value of 1 turns on Engine debug statements, a value of 2 adds interface debug statements as well. A value of 0 or the absence of the _debugging parameter yields normal operation.
There are four parts to the configuration. The first is to compile the interface, the second is to setup the path settings, third is to test the interface and the fourth is to make sure that MatLab is installed locally. The scripts for compiling the interface are located in VS_AR/bin diretcory. The zip file is called Matlab Interface_Comp.zip. The scripts are for Windows, Windows 64-bit, Linux and Mac OS/X.
Confirm that file "Engine.class" is generated in directory <VisualSim Architect Installation>/VS_AR/VisualSim/matlab
Confirm that file "ptmatlab.h" is generated in directory <VisualSim Architect Installation>/VS_AR/VisualSim/matlab
For Windows only, do the following:
In directory <VisualSim Architect Installation>/VS_AR/VisualSim/matlab, compile "ptmatlab.cc" using the following line:
Linux: g++ -shared \
-I<MATLAB>/extern/include \
-I<JAVA_HOME>/include \
-I<JAVA_HOME>/include/<OS> \
ptmatlab.cc \
-fno-exceptions \
-o libptmatlab.so \
-L<MATLAB>/bin/<OS> \ /* Note that this location should have libraries libmex.so, libmx.so, and libeng.so */
-fPIC -lc -lmex -lmx -leng
OSX: g++ -shared \
-I<MATLAB>/extern/include \
-I<JAVA_HOME>/Home/include \
ptmatlab.cc \
-fno-exceptions \
-o libptmatlab.so \
-L<MATLAB>/bin/maci64 \
-fPIC -lc -lmex -lmx -leng
CentOS Linux: g++ -shared \
-I<MATLAB>/extern/include \
-I<JAVA_HOME>/include \
-I<JAVA_HOME>/include/linux \
ptmatlab.cc \
-fno-exceptions \
-o libptmatlab.so \
-L<MATLAB>/bin/glnxa64 \
-fPIC -lc -lmex -lmx -leng
Windows: cl /EHsc -I"<Matlab install Directory>/extern/include" -I"<Java Installation Path>/include/win32" -I"<Java Installation Path>/include" -I"%CCDIR%/include" -I"%CCDIR%/lib" /Tpptmatlab.cc /Feptmatlab.dll /link /DLL /LIBPATH: "<Matlab install Directory>/extern/lib/win32/microsoft" libmx.lib libeng.lib libmex.lib
Windows using Visual Studio 13: cl /EHsc -I"%MATLAB_HOME%\extern\include" -I"%JAVA_HOME%\include\win32" -I"%JAVA_HOME%\include" -I"%CCDIR%\include" -I"%CCDIR%\lib" /Tpptmatlab.cc /Feptmatlab.dll /link /DLL /LIBPATH:"%MATLAB_HOME%\extern\lib\win32\microsoft" libmx.lib libeng.lib libmex.lib
Windows 64-bit: cl /EHsc -I"%MATLAB_HOME%\extern\include" -I"%JAVA_HOME%\include\win32" -I"%JAVA_HOME%\include" -I"%CCDIR%\include" -I"%CCDIR%\lib\amd64" /Tpptmatlab.cc /Feptmatlab.dll /link /DLL /LIBPATH:"%MATLAB_HOME%\extern\lib\win64\microsoft" libmx.lib libeng.lib libmex.lib /NODEFAULTLIB:"%CCDIR%\lib" libcmtd.lib libcmt.lib msvcrtd.lib
The following configuration items must be completed to make the MatLab interface work. These configuration changes can be made to the VisualSim.bat (Windows) or VisualSim.sh (Linux/OSX).
Both "PATH" and "CLASSPATH" must contain paths to the following:
Run the demonstration models to make sure your interface is working fine. If you have problems, contact Mirabilis Design support.
To use .m file with your VisualSim model, run the Example: VisualSim/simulators/wireless/demo/MatlabWirelessSoundDetection.xml
Note that during the preinitialize phase there may be a delay while the Matlab engine starts up over the network.
Be sure that you can run Matlab outside of VisualSim. This will test that Matlab is properly installed on your machine and that you have a license.
Issue #1: undefined symbol: mxSetName
During compilation or running a model, you will get the following undefined symbol error:
System.load(library): VisualSim/matlab/ptmatlab <JAVA_PATH>/bin/java: symbol lookup error: <VisualSim>/VS_AR/VisualSim/matlab/libptmatlab.so: undefined symbol: mxSetName
If you get this error, make sure that the following three define statements in ptmatlab.cc are uncommented – then recompile to build ptmatlab.dll
//#define PT_NO_ENGGETARRAY
//#define PT_NO_ENGPUTARRAY
//#define PT_NO_MXGETNAME
For additional errors on Linux, click here.
Parameter |
Explanation |
Type |
expression |
The parameter that is evaluated to produce the output. Typically, this parameter evaluates an expression involving the inputs. To refer to parameters in scope within the expression, use $name or ${name}, where "name" is the name of the parameter. |
String |
If true (checked), 1x1 matrix results are converted to ScalarTokens instead of a 1x1 MatrixToken, default is true. |
- |
|
If true, all double-valued matrix results are checked to see if all elements represent integers, and if so, an IntMatrixToken is returned, default is false for performance reasons. |
- |
Port |
Explanation |
output |
The output port. |
Created with the Personal Edition of HelpNDoc: Full-featured Help generator