標籤:參考 .com create user ready 奇異值分解 running mission rem
可以參考官方的說明文檔:
http://cn.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for-python.html
MATLAB Engine API的使用文檔:
http://cn.mathworks.com/help/matlab/matlab-engine-for-python.html
原材料:
1、MATLAB 2015a 32位的
2、Python 2.7.13 32位的
安裝:
1、運行cmd,切換到matlab的目錄下:
C:\Program Files (x86)\MATLAB\MATLAB Production Server\R2015a\extern\engines\python
由於這個檔案夾是在C盤的,安裝時有可能會遇到寫入權限的問題。如果出現write permission,可以通過右擊該檔案夾,選擇 屬性->安全->編輯,給目前使用者賦予完全控制的許可權。
> python setup.py install
即可完成安裝。
2、python中調用matlab的API樣本:
#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
在Python中建立MATLAB數組
1、建立1XN數組
import matlab.engineA = matlab.int8([1,2,3,4,5])print type(A),A.size,A#輸出:<class ‘matlab.mlarray.int8‘> (1, 5) [[1,2,3,4,5]]
Attribute or Method |
Purpose |
size
|
Size of array returned as a tuple |
reshape(size)
|
Reshape array as specified by sequence size |
2、建立多維陣列
import matlab.engineA = matlab.double([[1,2,3,4,5], [6,7,8,9,10]])print(A)
3、在Python中索引MATLAB數組
這裡的MATLAB數組索引跟在MATLAB的IDE裡面不一樣,MATLAB是從1開始,而在Python中是從0開始索引
import matlab.engineA = matlab.int8([1,2,3,4,5])print(A[0])#輸出:[1,2,3,4,5]
由於A是1X5的矩陣,A[0]就是[1,2,3,4,5],如果要在A裡面索引出4,則需要輸入:
print A[0][3]
4、在Python中對MATLAB數組切片
這裡文法跟Python中沒多少差別,直接使用即可
import matlab.engineA = matlab.int8([1,2,3,4,5])print(A[0][1:4])#輸出:[2,3,4]
切片賦值,也可以從一個MATLAB數組賦值到另一個MATLAB數組:
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]]
注意:
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 the 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]]
數組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]]
Python中MATLAB支援的資料類型:
matlab Class
|
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.int64[a]
|
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 matlab.object to Python. |
[a] In Python 2.7 on Windows, matlab.int64 is converted to int32 in MATLAB. Also, MATLAB cannot return an int64 array to Python. [b] In Python 2.7 on Windows, matlab.uint64 is converted to uint32 in MATLAB. Also, MATLAB cannot return a uint64 array to Python. [c] Logicals cannot be made into an array of complex numbers. |
奇異值分解樣本:
#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, you 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 that is already running on your local machine.
Call MATLAB Functions from Python
How to return an output argument from a MATLAB function. How to read multiple outputs from a function. What to do when 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 MATLAB Engine Workspace in Python
This example shows how to add variables to the MATLAB engine workspace in Python.
Use MATLAB Handle Objects in Python
This example shows how to create an object from a MATLAB handle class and call its methods in Python.
Use MATLAB Arrays in Python
This example shows how 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 how 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 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 matlab that enables you to call MATLAB functions from Python.
System Requirements for MATLAB Engine API for Python
What you need to write and build MATLAB engine applications.
Pass Data to MATLAB from Python
When you pass Python data as input arguments to MATLAB functions, the MATLAB Engine for Python converts the data into equivalent 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 data types.
MATLAB Arrays as Python Variables
The matlab Python package provides array classes to represent arrays of MATLAB numeric types as Python variables so that MATLAB 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 on 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 by MATLAB.
[Python-MATLAB] 在Python中調用MATLAB的API