当前位置: 首页 > news >正文

python如何调用matlab_[Python-MATLAB] 在Python中调用MATLAB的API

可以参考官方的说明文档:

MATLAB Engine API的使用文档:

原材料:

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

#输出:

(1, 5) [[1,2,3,4,5]]

Attribute or MethodPurpose

size

Size of array returned as a tuple

reshape(size)

Reshape array as specified by sequence size

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支持的数据类型:

matlab ClassConstructor 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-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

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

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.

Options for starting the MATLAB Engine for Python.

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

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.

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

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

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

This example shows how to add variables to the MATLAB engine workspace 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.

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.

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

Concepts

The MATLAB Engine API for Python provides a Python package named matlab that enables you to call MATLAB functions from Python.

What you need to write and build MATLAB engine applications.

When you pass Python data as input arguments to MATLAB functions, the MATLAB Engine for Python converts the data into equivalent MATLAB data types.

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

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.

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

Troubleshooting

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

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

相关文章:

  • 语料库与python应用_语料库与Python应用/语料库翻译学文库
  • 多个id如何用js_将多个MSA连超级高铁网络,如何用最少的轨道连接所有MSA?
  • python上传excel文件_利用django如何解析用户上传的excel文件
  • js悬浮二级菜单代码_纯CSS实现简单二级导航下拉效果
  • microbit python扩展_【micro:bit扩展】如何用慧编程扩展设计器为 micro:bit 编写扩展...
  • boost原理与sklearn源码_从sklearn源码简析GBDT
  • 信息隐藏将txt文件合并到jpg文件中_GIS工作中让你事半功倍,在数据处理中常用的小技巧...
  • android欢迎界面引导页_uni-app: 引导页功能如何实现?
  • 六位小数的字符串怎么转化成double类型而不损失精度?_C# 一次数据类型强转失败的翻车原因分析...
  • 互动整合营销_企业做整合营销,有什么实际的意义
  • vue+bootstrap响应式布局_实现 Vue 的响应式系统
  • python扫雷代码源码_利兹联足球俱乐部 2018
  • java写入txt文件_Java面试题如何将字符串写入文件?
  • python支持面向过程_python之面向过程,函数式编程,面向对象浅析
  • 小组取什么名字好_寓意好的公司名字大全 公司名字取什么好
  • 【笔记】你不知道的JS读书笔记——Promise
  • 【面试系列】之二:关于js原型
  • 2017年终总结、随想
  • Docker下部署自己的LNMP工作环境
  • download使用浅析
  • exif信息对照
  • If…else
  • js如何打印object对象
  • yii2中session跨域名的问题
  • 番外篇1:在Windows环境下安装JDK
  • 翻译:Hystrix - How To Use
  • 基于Android乐音识别(2)
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 微信公众号开发小记——5.python微信红包
  • 微信小程序填坑清单
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 优秀架构师必须掌握的架构思维
  • 原生Ajax
  • 再次简单明了总结flex布局,一看就懂...
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • ​【C语言】长篇详解,字符系列篇3-----strstr,strtok,strerror字符串函数的使用【图文详解​】
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (SpringBoot)第七章:SpringBoot日志文件
  • (篇九)MySQL常用内置函数
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (三分钟)速览传统边缘检测算子
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (算法)求1到1亿间的质数或素数
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • *Algs4-1.5.25随机网格的倍率测试-(未读懂题)
  • .gitignore
  • .net 生成二级域名
  • .NET 中创建支持集合初始化器的类型
  • .net的socket示例