[Python-matlab] API for invoking MATLAB in Python

Source: Internet
Author: User
Tags shallow copy square root python list

Refer to the official documentation:

Http://cn.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for-python.html

Usage documentation for MATLAB Engine API:

Http://cn.mathworks.com/help/matlab/matlab-engine-for-python.html

Raw materials:

1, MATLAB 2015a 32-bit

2, Python 2.7.13 32-bit

Installation:

1, run cmd, switch to the directory of MATLAB:

C:\Program Files (x86) \matlab\matlab Production Server\r2015a\extern\engines\python

Since this folder is in the C drive, there may be a problem with write permissions at the time of installation. If Write permission appears, you can give the current user Full control by right-clicking the folder, choosing Properties---security--edit.

> Python setup.py Install

To complete the installation.

2. Examples of API calls to Matlab in Python:

#coding =utf-8import Matlab.engineif __name__ = = ' __main__ ':    eng = Matlab.engine.start_matlab (' matlab_r2015a ')    A = ENG.SQRT (4.0)    print type (a), a    eng.quit ()    Pass

Creating an array of matlab in Python

1. Create a 1XN array

Import Matlab.enginea = Matlab.int8 ([1,2,3,4,5]) print type (A), a.size,a# output: <class ' Matlab.mlarray.int8 ' > (1, 5) [ [1,2,3,4,5]]

Attribute or Method

Purpose

size

Size of array returned as atuple

reshape(size)

Reshape array as specified by sequencesize

2. Creating multidimensional arrays

Import Matlab.enginea = Matlab.double ([[[1,2,3,4,5], [6,7,8,9,10]]) print (A)

3. Index the MATLAB array in python

The MATLAB array index here is not the same as in the MATLAB IDE, MATLAB is starting from 1, and in Python is starting from 0 index

Import Matlab.enginea = Matlab.int8 ([1,2,3,4,5]) print (a[0]) #输出: [1,2,3,4,5]

Because A is a 1x5 matrix, a[0] is [1,2,3,4,5], and if you want to index 4 in a, you need to enter:

Print A[0][3]

  

4. Slicing the MATLAB array in python

There's not much difference between the syntax and Python, just use it.

Import Matlab.enginea = Matlab.int8 ([1,2,3,4,5]) print (A[0][1:4]) #输出: [2,3,4]

Slice assignment or you can assign a value from one MATLAB array to another MATLAB array:

A = Matlab.double ([[1,2,3,4],[5,6,7,8]]); A[0] = [10,20,30,40]print (A) #输出: [[10.0,20.0,30.0,40.0],[5.0,6.0,7.0,8.0]]a = Matlab.int8 ([1,2,3,4,5,6,7,8]); A[0][2:4] = [30,40]a[0][6:8] = [70,80]print (A) #输出: [[1,2,30,40,5,6,70,80]]

Attention:

Note:   slicing MATLAB arrays behaves differently from slicing a Python list. Slicing a MATLAB array returns a view instead of a shallow copy. Given a MATLAB array and a Python list with the same values, assigning a slice results in different results as shown by th E following code. A = Matlab.int32 ([[1,2],[3,4],[5,6]]) L = [[1,2],[3,4],[5,6]]a[0] = a[0][::-1]l[0] = L[0][::-1]print (a) [[2,2],[3,4],[ 5,6]]print (L) [[2, 1], [3, 4], [5, 6]]

Array reshape

Import Matlab.enginea = Matlab.int8 ([1,2,3,4,5,6,7,8,9]) A.reshape ((3,3)) print (A) [[1,4,7],[2,5,8],[3,6,9]]

data types supported by MATLAB in Python:

matlabClass

Constructor Call in Python

matlab.double

matlab.double(initializer=None, size=None, is_complex=False)

matlab.single

matlab.single(initializer=None, size=None, is_complex=False)

matlab.int8

matlab.int8(initializer=None, size=None, is_complex=False)

matlab.int16

matlab.int16(initializer=None, size=None, is_complex=False)

matlab.int32

matlab.int32(initializer=None, size=None, is_complex=False)

matlab.int64A

matlab.int64(initializer=None, size=None, is_complex=False)

matlab.uint8

matlab.uint8(initializer=None, size=None, is_complex=False)

matlab.uint16

matlab.uint16(initializer=None, size=None, is_complex=False)

matlab.uint32

matlab.uint32(initializer=None, size=None, is_complex=False)

matlab.uint64[b]

matlab.uint64(initializer=None, size=None, is_complex=False)

matlab.logical

matlab.logical(initializer=None, size=None)C

matlab.object

No constructor. When a-function returns a handle to a MATLAB object, the engine returns a to matlab.object Python.

[A] in Python 2.7 on Windows, was converted to in matlab.int64 int32 MATLAB. Also, MATLAB cannot return an int64 array to Python.

[b] in Python 2.7 on Windows, are converted to in matlab.uint64 uint32 MATLAB. Also, MATLAB cannot return a uint64 array to Python.

[C] logicals cannot be made to an array of complex numbers.

Examples of singular value decomposition:

#coding =utf-8import matlab.enginefrom numpy import *if __name__ = = ' __main__ ':    eng = Matlab.engine.start_matlab (' matlab_r2015a ')    A = matlab.double ([[[1,2],[5,6]])    print type (a), A.size,a    print Eng.eig (a)    eng.quit ( )    Pass

  

Examples and how To

Install MATLAB Engine API for Python

To start the MATLAB engine within a Python session, your first must install the engine API as a python package.

Install MATLAB Engine API for Python in nondefault Locations

By default, the installer builds the engine API for Python in the matlabroot\extern\engines\python folder. The installer installs the engine in the default Python folder.

Start and Stop MATLAB Engine for Python

Options for starting the MATLAB Engine for Python.

Connect Python to Running MATLAB Session

How to connect the MATLAB Engine for Python to a shared MATLAB session, which is already running on your local machine.

Call MATLAB Functions from Python

How to return the output argument from a MATLAB function. How to read multiple outputs from a function. What does when is the MATLAB function does not return an output argument.

Call MATLAB Functions asynchronously from Python

This example shows how to call the MATLAB sqrt function asynchronously from Python and retrieve the square root later.

Call User Script and Function from Python

This example shows how to call a MATLAB script to compute the area of a triangle from Python.

Redirect Standard Output and Error to Python

This example shows how to redirect standard output and standard error from a MATLAB function to Python StringIO objects.

Use of MATLAB Engine Workspace in Python

This example shows what to add variables to the MATLAB engine workspace in Python.

Use of MATLAB Handle Objects in Python

This example shows what to create an object from a MATLAB handle class and call its methods in Python.

Use of MATLAB Arrays in Python

This example shows what to create a MATLAB array in Python and pass it as the input argument to the MATLAB sqrt function.

Sort and Plot MATLAB Data from Python

This example shows what to sort data about patients into lists of smokers and nonsmokers in Python and plot blood pressure Readings for the patients with MATLAB.

Get Help for MATLAB Functions from Python

From Python, you can access the supporting documentation for all MATLAB functions.

Concepts

Get Started with MATLAB Engine API for Python

The Matlab Engine API for Python provides a python package named this enables you-call matlab MATLAB functions from Pyth Mnl

System Requirements for MATLAB Engine API for Python

What are need to write and build MATLAB engine applications.

Pass Data to MATLAB from Python

When your pass Python data as input arguments to MATLAB functions, the MATLAB Engine for Python Converts the data into Equi Valent MATLAB data types.

Handle Data returned from MATLAB to Python

When MATLAB functions return output arguments, the Matlab Engine API for Python converts the data into equivalent Python D ATA types.

MATLAB Arrays as Python Variables

The matlab Python package provides arrays classes to represent arrays of MATLAB numeric types as Python variables so that MA Tlab arrays can be passed between Python and MATLAB.

Default Numeric Types in MATLAB and Python

MATLAB stores all numeric values as double-precision floating point numbers by default.

Troubleshooting

Limitations to MATLAB Engine API for Python

The engine cannot start or connect to MATLAB in a remote machine.

Troubleshoot MATLAB Errors in Python

When a MATLAB function raises an error, the MATLAB Engine for Python stops the function and catches the exception raised B Y MATLAB.

  

  

[Python-matlab] API for invoking MATLAB in Python

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.