|
| 1 | +.. _matlab: |
| 2 | + |
| 3 | +****************** |
| 4 | +MATLAB Integration |
| 5 | +****************** |
| 6 | + |
| 7 | +.. note:: |
| 8 | + |
| 9 | + MATLAB does not support Tkinter, which disables matplotlib. This means that :meth:`~magpylib.Collection.displaySystem()` will not generate a display and might interrupt the program. |
| 10 | + |
| 11 | + |
| 12 | +Setting Python Interpreter |
| 13 | +########################### |
| 14 | + |
| 15 | +As of version R2015b, MATLAB allows you to call libraries from other programming languages, including Python, which enables users to run magpylib from the MATLAB interface. The following guide intends to provide a digest of the `Official MATLAB documentation <https://www.mathworks.com/help/matlab/call-python-libraries.html>`_ with a focus on utilizing this interface with magpylib. |
| 16 | + |
| 17 | +Running ``>>> pyversion`` following line in the MATLAB console tells you which Python environment (user space interpreter) is connected to your MATLAB interface. |
| 18 | + |
| 19 | +If magpylib is already installed in this environment you can directly call it, as shown in the `Example`_ below. If not please follow the :ref:`installation` instructions and install magpylib. |
| 20 | + |
| 21 | +If you choose to install magpylib in a different environment than the one that is currently connected to your MATLAB interpreter, use the following command in the MATLAB console to connect the new environment instead (choose correct path pointing at your Python interpreter). |
| 22 | + |
| 23 | +.. code-block:: matlab |
| 24 | + |
| 25 | + >>> pyversion C:\Users\...\AppData\Local\Continuum\anaconda3\envs\magpy\python.exe |
| 26 | +
|
| 27 | +
|
| 28 | +Example |
| 29 | +############ |
| 30 | + |
| 31 | +The following MATLAB 2019 script showcases most functionalities. |
| 32 | + |
| 33 | +.. code-block:: matlab |
| 34 | +
|
| 35 | + %%%%%%%%%%%%%%%%%% magpytest.m %%%%%%%%%%%%%% |
| 36 | + %% Showcase Python + MATLAB Interoperability. |
| 37 | + %% Define and calculate the field of a |
| 38 | + %% Cuboid magnet inside a Collection. |
| 39 | + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 40 | +
|
| 41 | + %% Import the library |
| 42 | + py.importlib.import_module("magpylib") |
| 43 | +
|
| 44 | + %% Define Python types for input |
| 45 | + vec3 = py.list({1,2,3}) |
| 46 | + scalar = py.int(90) |
| 47 | +
|
| 48 | + %% Define input |
| 49 | + mag = vec3 |
| 50 | + dim = vec3 |
| 51 | + angle = scalar |
| 52 | + sensorPos = vec3 |
| 53 | +
|
| 54 | + %% Execute Python |
| 55 | + % 2 positional and 1 keyword argument in Box |
| 56 | + box = py.magpylib.source.magnet.Box(mag,dim,pyargs('angle',angle)) |
| 57 | + col = py.magpylib.Collection(box) |
| 58 | + pythonResult = col.getB(sensorPos) |
| 59 | +
|
| 60 | + %% Convert Python Result to MATLAB data format |
| 61 | + matlabResult = double(pythonResult) |
| 62 | +
|
| 63 | +.. note:: |
| 64 | + With old versions of Matlab the *double(pythonResult)* type conversion might give an error message. |
0 commit comments