{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Matlab engine\n\nThis demo shows how to communicate with the Matlab engine.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "try:\n import matlab\nexcept Exception as ex:\n print(\"itom is possibly compiled without Matlab support. This demo is not working\")\n raise ex\n\nfrom itom import dataObject" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Matlab console is opened\nMatlab can only be properly loaded if the libraries libeng.dll and libmx.dll (or libeng.so and libmx.so under linux)\ncan be properly found in the PATH of the operating system. An itom x64 also requires a Matlab x64 version and vice-versa.\nRe-login to your computer after having changed the PATH variable, if Qt is also contained in the PATH variable,\nput the Matlab path after Qt since the bin folder of Matlab also contains old Qt libraries.\nIf the matlab libraries could be loaded but the session could not be started, also see this link (for Windows users):\nhttp://de.mathworks.com/help/matlab/matlab_external/register-matlab-as-automation-server.html.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "session = matlab.MatlabSession()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Creates the string variable 'myString' in the Matlab workspace having the value 'test'.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "session.setString(\"myString\", \"test\")\nprint(\"myString:\", session.getString(\"myString\")) # returns 'test' as answer in tiom" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Creates a 2x3 random matrix in Matlab (name: myArray).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "session.setValue(\"myArray\", dataObject.randN([2, 3], \"int16\")) \narr = session.getValue(\"myArray\") # returns the 2x3 array 'myArray' from Matlab as Numpy array\nprint(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read the current working directory of matlab.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "session.run(\"curDir = cd\")\nprint(session.getString(\"curDir\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run directly executes the command (as string). This is the same than typing this command into the command line of Matlab.\nuse this to also execute functions in Matlab. At first, send all required variables to the Matlab workspace, then execute a function\nthat uses these variables.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "del session # closes the session and deletes the instance\n\n# session.close() only closes the session" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }