[Python-MATLAB] 在Python中调用MATLAB的API
可以参考官方的说明文档:
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-8
import matlab.engine if __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.engine
A = 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 of array returned as a |
|
Reshape array as specified by sequence |
2、创建多维数组
import matlab.engine
A = 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.engine
A = 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.engine
A = 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.engine
A = 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支持的数据类型:
|
Constructor Call in Python |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
No constructor. When a function returns a handle to a MATLAB object, the engine returns a |
[a] In Python 2.7 on Windows, [b] In Python 2.7 on Windows, [c] Logicals cannot be made into an array of complex numbers. |
奇异值分解示例:
#coding=utf-8
import matlab.engine
from 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
folder. The installer installs the engine in the default Python folder.matlabroot
\extern\engines\python
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.
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的更多相关文章
- Java中调用MatLab返回值
当在Java中使用MatLab函数时,由于语言语法的不同,Matlab返回多个数据时,想在Java中获取到并进行使用.查阅了网上资料,翻箱倒柜加上自己实战,得出方法如下: 如MatLab函数返回的是N ...
- 一文教你快速学会在matlab的simulink中调用C语言进行仿真
本文介绍如何在matlab的simulink中嵌入C语言进行多输入多输出的仿真:matlab版本位2015b: 创作不易,如果本文帮到了您: 如果本文帮到了您,请帮忙点个赞
- 如何在Python中调用Matlab
检查您的系统是否具有受支持的 Python 版本和 MATLAB R2014b 或更新版本.要检查您的系统上是否已安装 Python,请在操作系统提示符下运行 Python. 1)打开Prompt,输 ...
- C#中调用Matlab人工神经网络算法实现手写数字识别
手写数字识别实现 设计技术参数:通过由数字构成的图像,自动实现几个不同数字的识别,设计识别方法,有较高的识别率 关键字:二值化 投影 矩阵 目标定位 Matlab 手写数字图像识别简介: 手写 ...
- 【转】三种方式在C++中调用matlab
C/C++调用Matlab 在工程实践中,C/C++调用Matlab 的方法主要有调用Matlab 计算引擎.包含m 文件转 换的C/C++文件,以及调用m文件生成的DLL 文件. 1 利用Mat ...
- 在代码中调用 mvc 4 api
mvc 4 api 的调用有很多种,最常见也最简单的一种是 用 ajax 的方式在前端界面中调用, 如果是在后台代码中调用 ,是要复杂一些,以下是 以 post 的方式调用 api 的封装好的方法: ...
- python 在.py文件中调用其他.py内的函数
假设名为A.py的文件需要调用B.py文件内的C(x,y)函数 假如在同一目录下,则只需 import B if __name__ == "__main__": B.C(x,y ...
- python 中调用windows系统api操作剪贴版
# -*- coding: utf-8 -*- ''' Created on 2013-11-26 @author: Chengshaoling ''' import win32clipboard a ...
- 在C中调用Matlab (转)
http://blog.163.com/rongting_chen/blog/static/164906844201252354518462/ http://www.ilovematlab.cn/th ...
随机推荐
- grunt---grunt_test 测试用例
说明: http://www.gruntjs.net/getting-started --grunt快速入门(创建package.json和Gruntfile.js准备一份新的 Grunt 项目一般需 ...
- ecplise建立模拟器,安装apk文件
方法一,把所要安装的apk,例xxx.apk拷贝到sdk下的adb的路径下,也就是和adb在同一个文件夹,比如我的是D:\Program Files\Android\sdk\platform-tool ...
- 九度oj 题目1086:最小花费
题目描述: 在某条线路上有N个火车站,有三种距离的路程,L1,L2,L3,对应的价格为C1,C2,C3.其对应关系如下: 距离s 票价 0<S<=L1 ...
- nginx报错504
Nginx 504错误(Gateway time-out 网关超时)的含义是所请求的网关没有请求到,简单来说就是没有请求到可以执行的PHP-CGI. 一般看来, 这种情况可能是由于nginx默认的f ...
- spring运行时没有问题,在单元测试时,出现java.lang.ClassFormatError错误
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstrac ...
- 使用Unity做2.5D游戏教程(一)
最近在研究Unity 3D,看了老外Marin Todorov写的教程很详细,就翻译过来以便自己参考,翻译不好的地方请多包涵. 如果你不了解2.5D游戏是什么,它基本上是个3D游戏而你可以想象是压扁的 ...
- UVA 11297 Census ——二维线段树
[题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> # ...
- 转 CListCtrl::InsertColumn、InsertItem、SetItemText;
将数据写入到CListCtrl 向CListCtrl中写入数据,一般使用3个成员方法: CListCtrl::InsertColumn; CListCtrl::InsertItem; CListCtr ...
- bzoj4002 [JLOI2015]有意义的字符串 快速幂
Description B 君有两个好朋友,他们叫宁宁和冉冉. 有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求((b+sqrt(D)/2)^N的整数部分,请输出结果 Mod 752844341 ...
- bzoj1853: [Scoi2010]幸运数字 dp+容斥原理
在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运号码”总是 ...