Python extension interface [3], Matlab engine, using Python to invoke MATLAB program

Source: Internet
Author: User
Tags shallow copy python list

Python-matlab

Directory

    1. Python-matlab engine
    2. Python-matlab Array
    3. Python-matlab Basic Operation
    4. Python-matlab Call M file

The official MATLAB documentation describes the engine interface between MATLAB and the rest of the programming languages, including the Open engine API for Python, which can be consulted in the official tutorials, including engine installation, basic usage, and data type conversion and interaction between Python and Matlab.

In addition to using the website's MATLAB engine to drive Matlab, you can also use a third-party package Mlab to connect or directly use the win32com Dispatch for control, but currently Mlab only supports Python version 2.

1 Python-matlab engine /Pyhton-matlab Engine

First, you need to ensure the configuration and installation of MATLAB and Python, using the setup.py file provided by Matlab to install the Python engine package, installation procedures and procedures are as follows,

1. Make sure that the available Python and Matlab are installed, and that the two versions correspond to 32-bit Python for 32-bit MATLAB, as well as a Python version supported by MATLAB (currently supported in version 2015a for Python versions 2.7/3.3/ 3.4);

2. Add the Python directory to the environment variable (if not added);

3. Get the Matlab folder directory, you can enter the Matlabroot command through the MATLAB command Line window to return;

4. Install the engine, Windows will install using the following command (where the path may need to be modified), where Administrator privileges may be required to run.

1 CD C:\Program Files\matlab\r2015a\extern\engines\python   2 Install  3 Pause  

2 Python-matlab Array /Pyhton-matlab Array

In Python, if you need to create an array of MATLAB, you can also do it through the MATLAB engine API, where the main data types are displayed.

The basic use of arrays is described below, and its basic usage is similar to NumPy, but the reshape () function is slightly different,

1 ImportMatlab.engine2 3 #Basic Usage4Int_8 = Matlab.int8 ([1, 2, 3, 4, 5, 6])5 Print(Int_8)#[[1, 2, 3, 4, 5, 6]6 Print(int_8.size)#(1, 6)7Int_8.reshape ((2, 3))#reshape function is different from NumPy8 Print(Int_8)#[ [1, 3, 5], [2, 4, 6]9 TenDouble = Matlab.double ([[1, 2, 3], [4, 5, 6]]) One Print(double)#[ [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] A Print(Double[0])#[1.0, 2.0, 3.0] - Print(Double[1][2])#6.0

For arrays, the array of MATLAB differs from the Python list, which is explained by the fact that the MATLAB array slice returns a view instead of returning a shallow copy as in Python.

1 #Slice Array2PY = [[1, 2, 3], [4, 5, 6]]3MT = Matlab.int32 ([[1, 2, 3], [4, 5, 6]])4Py[0] = py[0][::-1]5Mt[0] = mt[0][::-1]6 #slicing a Matlab array returns a view instead of a shallow copy7 Print(PY)#[ [3, 2, 1], [4, 5, 6]8 Print(MT)#[ [3, 2, 3], [4, 5, 6]

3 Python-matlab Basic Operations /Pyhton-matlab Basic operation

Python can also perform some basic operation and control of MATLAB through the engine.

Full code

1 ImportMatlab.engine2 3ENG =Matlab.engine.start_matlab ()4 5 Print(Eng.sqrt (4.))#2.06Eng.plot (Matlab.int32 ([1, 2, 3, 4]), Matlab.int32 ([1, 2, 3, 4]))7 8Eng.eval (" on", nargout=0)9Eng.eval ("plot ([4, 3, 2, 1], [1, 2, 3, 4])", nargout=0)Ten  OneEng.eval ("x = 3", nargout=0) AEng.eval ("y = x", nargout=0) -Eng.eval ("z = [213, 123]", nargout=0) - Print(Eng.workspace) the Print(eng.workspace['x'], eng.workspace['Z']) - """ - Name Size Bytes Class Attributes -  + x 1x1 8 Double - y 1x1 8 double + Z 1x2 Double A  at 3.0 [[213.0,123.0]] - """ -  -Input"Press Enter to exit.") -Eng.quit ()
View Code

Segmented interpretation

1 Import Matlab.engine 2 3 eng = Matlab.engine.start_matlab ()

First, import the required package and generate the instance, call the sqrt () function calculation here, get the result, and use the engine instance to call the plot function to paint, but note that the parameters passed in need to be MATLAB type parameters.

1 Print (Eng.sqrt (4.))     # 2.0 2 Eng.plot (Matlab.int32 ([1, 2, 3, 4]), Matlab.int32 ([1, 2, 3, 4]))

When we need to execute some MATLAB command, we can use the Eval function to enter it, the following method draws another line, where the nargout parameter is the number of parameters to set the output return, the default is 1. When no parameter is returned, it needs to be set to 0.

1Eng.eval (" on", nargout=0)2Eng.eval ("plot ([4, 3, 2, 1], [1, 2, 3, 4])", nargout=0)3 4Eng.eval ("x = 3", nargout=0)5Eng.eval ("y = x", nargout=0)6Eng.eval ("z = [213, 123]", nargout=0)7 Print(Eng.workspace)8 Print(eng.workspace['x'], eng.workspace['Z'])9 """Ten Name Size Bytes Class Attributes One  A x 1x1 8 Double - y 1x1 8 double - Z 1x2 Double the  - 3.0 [[213.0,123.0]] - """ -  +Input"Press Enter to exit.") -Eng.quit ()

4 Python-matlab called m file /Pyhton-matlab call M File

Here's how to use Python to call M to calculate and get the results returned, first define the following M file, in the called M file to call the next m file, using the M file as follows,

Define the entry function Callentry, receive two parameters, then add and multiply the two parameters internally, then call the Callsub function of the external m file to subtract, and save the returned result in the array R.

CALLENTRY.M Code

function [x, Y, z] == = = Callsub (A, b) Endfunction l = mul (m, n); l=m*N; Endfunction L = Add (m, n); l=m+n;end

CALLSUB.M Code

function r ==-b;end

In Python, run the following code,

1 Import Matlab.engine 2 3 eng = Matlab.engine.start_matlab ()4print(Eng.callentry (7.7, 2.1, nargout= 3))5 eng.quit ()

Note : It is worth noting that the nargout parameter needs to be set here, when not set by default is 1, that is, only 1 parameters are returned by default, when you know that Matlab returns the number of parameters, Set up with Nargout to get all the required parameters. When no parameters are returned, set to 0.

The first time you run the build instance will be slower, because you need to start the MATLAB engine, the final output is as follows, you can see that MATLAB console interface display results are also output in Python, the final result is a list of Python data.

x =      9.8000    =     16.1700    =      5.6000    =      9.8000   16.1700    5.6000    (9.8, 16.17, 5.6)  

Python extension interface [3], Matlab engine, using Python to invoke MATLAB program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.