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

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调用simulink_[Python-MATLAB] 在Python中调用MATLAB的API相关推荐

  1. c++调用python接口打包_在QT C++中调用 Python并将软件打包发布(裸机可运行)

    为了提高工作效率,需要一个可以自动生成多份相关联的word文档免去繁琐复制粘贴工作的软件.最后选定使用QT C++做界面和主要逻辑程序设计,对word的操作使用python写好对应的函数,然后在QT中 ...

  2. java 调用matlab函数_java中调用Matlab的函数+注意事项

    一.matlab版本必须支持java 在command 模式下面运行deploytool,如果支持该命令即可使用 二.matlab中function的书写 %定义一个函数operation(a,b), ...

  3. 【Matlab】Java中使用MATLAB作图

    最近做一个项目,需要很多进行很多信号处理--小魏就是学软件的,对信号处理简直是个小白,最简单的实现就是傻瓜似的调用MATLAB的各种工具箱,达到目的就行. 同时,MATLAB是种解释性语言,执行效率比 ...

  4. 调用内部类里,在静态类中调用动态方法的问题

    在编译写书上一个例子时,添加一个内部类时,出现了问题:"No enclosing instance of type GameSaverTest is accessible. Must qua ...

  5. php调用会员刷卡,dedecms模板中调用会员信息标签的方法

    dedecms模板中调用会员信息标签的方法 比如你想在DEDE首页或者内页里调用会员:用户名.昵称.性别.会员等级.注册邮箱.会员级别等信息.就需要我们本篇教程提供的调用标签了. 首先,我们打开文件 ...

  6. 【Unity3D】Android Studio 工程中使用 Java 代码调用 Unity 的 C# 脚本 ( Java 中调用 UnityPlayer#UnitySendMessage 方法 )

    文章目录 一. Java 调用 C# 依赖库准备 1.依赖库位置 2.unityLibrary 依赖库位置 二. Java 调用 C# 的 UnityPlayer#UnitySendMessage 方 ...

  7. qt 子类调用父类的函数_子类中调用父类的方法

    父类名 . __init__(self, ...) 可以将父类中的init中的属性重复调用,减少代码的重复 class Vehicle: def __init__(self, name, speed, ...

  8. c语言调用c 方法,C语言代码中调用C++代码的方法示例

    由于历史原因,以及不同开发人员的技术偏好,C语言和C++语言都有一些独有的非常有价值的项目,因而两种语言的互操作,充分利用前人造的轮子是一件非常有价值的事情. C++代码调用C代码很简单,只要分别在包 ...

  9. Python学习笔记之六:在VS中调用Python

    1,安装配置好Python本身的运行环境,以能在命令行下运行py脚本为准 2,将Python的根目录下的include文件夹,添加到VS的项目属性->配置属性->C/C++->&qu ...

  10. python执行linux命令返回结果_Python中调用Linux命令并获取返回值

    方法一.使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256/512表示未找到,该方法适用于she ...

最新文章

  1. Eclipse导入项目: No projects are found to import
  2. java final
  3. cocostudio 实现换行功能的label (文本区) lua
  4. USACO1.5 Number Triangles(numtri)
  5. doxygen问题集锦
  6. spring-mvc笔记1
  7. 20179311《网络攻防实践》第五周作业
  8. 在ubuntu linux 中编写一个自己的bash脚本
  9. Socket 使用笔记与注意事项(一)
  10. 4698. [SDOI2008]Sandy的卡片【后缀数组】
  11. 黄色叹号_平行进口车有质量问题?许多新车都有的黄色感叹号故障灯是什么?...
  12. matlab进化树的下载,MEGA官网下载|MEGA进化树 V7.0.26 官方最新版 下载_当下软件园_软件下载...
  13. java百度云盘,看完直呼内行
  14. 苗木损耗1.1用计算机怎么算,04园林绿化定额说明
  15. [总结]视音频编解码技术零基础学习方法
  16. windows电脑给苹果电脑下ipa包
  17. 我的HIFI WAV 播放器设计之二电路图数字电路部分
  18. linux命令返回结果保存到文件,Linux终端运行命令及结果同时保存入文件方法总结...
  19. js 判断两个时间相差多少月_js对日期操作 获取两个日期的相差是否在几月之内...
  20. homeassistant mysql_给Homeassistant更换PostgreSQL数据库

热门文章

  1. Boost.Bind的基础使用
  2. HTML中的表格和表单控件详解
  3. mysql 处理数据_MySQL数据库,如何处理重复的数据?
  4. xadmin获取mysql_Django+Xadmin构建项目的方法步骤
  5. python 放射 水平_基于Python的放射性核素大气扩散程序初步开发与验证
  6. edxposed显示未安装_为什么LED显示屏经常跳闸
  7. python开发工具下所有软件都打不开_Python中pip/setup安装插件失败提示“pypi.python.org” 打不开的解决办法...
  8. windows服务器查看系统异常,Windows服务器异常---查看系统日志--以蓝屏为例分析...
  9. mysql amoeba 事务_MySQL基于Amoeba实现读写分离
  10. python 调用另一个py_Python打包:setuptools与setup.py/.cfg入门简介