.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\others\demoMatlabEngine.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_others_demoMatlabEngine.py: Matlab engine ================ This demo shows how to communicate with the Matlab engine. .. GENERATED FROM PYTHON SOURCE LINES 5-14 .. code-block:: Python try: import matlab except Exception as ex: print("itom is possibly compiled without Matlab support. This demo is not working") raise ex from itom import dataObject .. GENERATED FROM PYTHON SOURCE LINES 16-23 A Matlab console is opened Matlab can only be properly loaded if the libraries libeng.dll and libmx.dll (or libeng.so and libmx.so under linux) can be properly found in the PATH of the operating system. An itom x64 also requires a Matlab x64 version and vice-versa. Re-login to your computer after having changed the PATH variable, if Qt is also contained in the PATH variable, put the Matlab path after Qt since the bin folder of Matlab also contains old Qt libraries. If the matlab libraries could be loaded but the session could not be started, also see this link (for Windows users): http://de.mathworks.com/help/matlab/matlab_external/register-matlab-as-automation-server.html. .. GENERATED FROM PYTHON SOURCE LINES 23-25 .. code-block:: Python session = matlab.MatlabSession() .. GENERATED FROM PYTHON SOURCE LINES 26-27 Creates the string variable 'myString' in the Matlab workspace having the value 'test'. .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: Python session.setString("myString", "test") print("myString:", session.getString("myString")) # returns 'test' as answer in tiom .. GENERATED FROM PYTHON SOURCE LINES 31-32 Creates a 2x3 random matrix in Matlab (name: myArray). .. GENERATED FROM PYTHON SOURCE LINES 32-38 .. code-block:: Python session.setValue("myArray", dataObject.randN([2, 3], "int16")) arr = session.getValue( "myArray" ) # returns the 2x3 array 'myArray' from Matlab as Numpy array print(arr) .. GENERATED FROM PYTHON SOURCE LINES 39-40 Read the current working directory of matlab. .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python session.run("curDir = cd") print(session.getString("curDir")) .. GENERATED FROM PYTHON SOURCE LINES 44-47 Run directly executes the command (as string). This is the same than typing this command into the command line of Matlab. use this to also execute functions in Matlab. At first, send all required variables to the Matlab workspace, then execute a function that uses these variables. .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: Python del session # closes the session and deletes the instance # session.close() only closes the session .. _sphx_glr_download_11_demos_python_packages_others_demoMatlabEngine.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demoMatlabEngine.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demoMatlabEngine.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demoMatlabEngine.zip `