adc网站建设,wordpress quick chat,农产品网站建设案例,计算机速成班培训Matlab的官方文档中介绍了 Matlab 与其余编程语言之间的引擎接口#xff0c;其中包括对于 Python 开放的引擎 API#xff0c;可参考官方教程#xff0c;其中包括引擎安装#xff0c;基本使用#xff0c;以及Python与Matlab之间的数据类型转换及交互。
在 Windows 系统中其中包括对于 Python 开放的引擎 API可参考官方教程其中包括引擎安装基本使用以及Python与Matlab之间的数据类型转换及交互。
在 Windows 系统中(可能需要管理员权限运行)
cd matlabroot\extern\engines\python
python setup.py install
在 Mac 或 Linux 系统中
cd matlabroot/extern/engines/python
python setup.py install
基础用法
下面介绍数组的基本使用其基本使用方法与 numpy 类似但是 reshape() 函数略有不同
import matlab
int_8 matlab.int8([1, 2, 3, 4, 5, 6])
print(int_8) # [[1, 2, 3, 4, 5, 6]]
print(int_8.size) # (1, 6)
int_8.reshape((2, 3)) # reshape function is different from numpy
print(int_8) # [[1, 3, 5], [2, 4, 6]]
double matlab.double([[1, 2, 3], [4, 5, 6]])
print(double) # [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
print(double[0]) # [1.0, 2.0, 3.0]
print(double[1][2]) # 6.0
对于数组的切片Matlab 的 array 与 Python 的 list 也有所不同官网给出的解释在于Matlab 数组切片返回的是一个视图而不是像 Python 中返回一个浅拷贝。
# Slice array
py [[1, 2, 3], [4, 5, 6]]
mt matlab.int32([[1, 2, 3], [4, 5, 6]])
py[0] py[0][::-1]
mt[0] mt[0][::-1]
# Slicing a Matlab array returns a view instead of a shallow copy
print(py) # [[3, 2, 1], [4, 5, 6]]
print(mt) # [[3, 2, 3], [4, 5, 6]]
Python的扩展接口 中介绍
Python 还可以通过引擎完成对 Matlab 的一些基本操作与控制。以下代码需要在终端运行
import matlab.engine
eng matlab.engine.start_matlab()
print(eng.sqrt(4.)) # 2.0
eng.plot(matlab.int32([1, 2, 3, 4]), matlab.int32([1, 2, 3, 4]))
eng.eval(hold on, nargout0)
eng.eval(plot([4, 3, 2, 1], [1, 2, 3, 4]), nargout0)
eng.eval(x 3, nargout0)
eng.eval(y 41, nargout0)
eng.eval(z [213, 123], nargout0)
print(eng.workspace)
print(eng.workspace[x], eng.workspace[z])Name Size Bytes Class Attributes
x 1x1 8 double
y 1x1 8 double
z 1x2 16 double
3.0 [[213.0,123.0]]input(Press Enter to exit.)
eng.quit()
Python-Matlab调用call m 文件
定义入口函数 callentry接收两个参数随后对两个参数分别在内部进行加和乘操作再调用外部另一个 m 文件的 callsub 函数进行相减操作将返回的结果保存在数组r中返回。
callentry.m 代码
function [x, y, z] callentry(a, b);
x add(a, b)
y mul(a, b)
z callsub(a, b)
end
function l mul(m, n);
lm*n;
end
function l add(m, n);
lmn;
end
callsub.m 代码
function r callsub(a, b);
r a-b;
end
在 Python 中运行如下代码
import matlab.engine
eng matlab.engine.start_matlab()
print(eng.callentry(7.7, 2.1, nargout3))
eng.quit()
Note: 值得注意的是此处需要设置 nargout 参数当未设置时默认为 1即默认只返回 1 个参数当知道 Matlab 返回参数的数量时通过nargout 进行设置来获取所有需要的参数。无参数返回时请设为 0。
在第一次运行生成实例时会较慢因为需要启动 Matlab 引擎最终得到输出如下可以看到Matlab 的 console 界面显示的结果在 Python 中也会输出最后得到的结果是列表形式的 Python 数据。
x
9.8000
y
16.1700
z
5.6000
r
9.8000 16.1700 5.6000
(9.8, 16.17, 5.6)
MATLAB 中 调用 Python
只要正确安装对应的 matlab 和 python一般就可以使用了不需要手动设置路径。
matlab 官方教程从 MATLAB 调用 Python
matlab 把所有参数输出到一个文件里然后用 system 命令调 python 脚本。python 脚本读文件做计算结果再写文件。最后 matlab 再读文件得到结果。假设 python 脚本的用法是
python xxx.py in.txt out.txt
则 matlab 调用的命令
[status, cmdout] system(python xxx.py in.txt out.txt)
Matlab 的 system 函数用来向操作系统发送一条指令并得到控制台的输出可以直接将控制台的输出在 Command Window 打印出来或者保存在变量中。 与 system 类似的还有 dos 函数和 unix 函数我觉得它们都是对 system 函数的一种包装而 Matlab 的 system 函数也许是对 C 的库函数system 的包装。
先编写一个调用 Python 脚本的 matlab 程序即 python.m
function [result status] python(varargin)
% call python
%命令字符串
cmdStringpython;
for i 1:nargin
thisArg varargin{i};
if isempty(thisArg) | ~ischar(thisArg)
error([All input arguments must be valid strings.]);
elseif exist(thisArg)2
%这是一个在Matlab路径中的可用的文件
if isempty(dir(thisArg))
%得到完整路径
thisArg which(thisArg);
end
elseif i1
% 第一个参数是Python文件 - 必须是一个可用的文件
error([Unable to find Python file: , thisArg]);
end
% 如果thisArg中有空格就用双引号把它括起来
if any(thisArg )
thisArg [, thisArg, ];
end
% 将thisArg加在cmdString后面
cmdString [cmdString, , thisArg]
end
%发送命令
[status,result]system(cmdString);
end
就可以用这个函数调用 python 脚本了。 下面就来个调用 python 脚本 matlab_readlines.py (保存在 matlab 当前目录)的例子
import sys
def readLines(fname):
try:
fopen(fname,r)
lif.read().splitlines()
cell{repr(li)[1:-1]}
f.close()
print cell
except IOError:
print Cant open file fname
if __main____name__:
if len(sys.argv)2:
print No file specified.
sys.exit()
else:
readLines(sys.argv[1])
这个脚本用来读取一个文本文件并生成 Matlab 风格的 cell 数组的定义字符串每个单元为文本的一行。 放了一个测试用的文本文件 test.txt 在Matlab 的 Current Directory 中内容如下
This is test.txt
It can help you test python.m
and matlab_readlines.py
测试
在 Matlab 的 Command Window 中输入
str python(matlab_readlines.py,test.txt);
eval([c str])
celldisp(c)
下面我举一个 python 转 matlab 的例子
HDF5 转 .mat
# 载入必备的库和数据
import tables as tb
import scipy.io as sio
h5 tb.open_file(E:/xdata/X.h5)
fm h5.root.fashion_mnist # 获取 fashion_mnist 数据
mdict {
testX:fm.testX[:].reshape((fm.testX.shape[0], -1)),
trainX:fm.trainX[:].reshape((fm.trainX.shape[0], -1)),
trainY:fm.trainY[:],
testY:fm.testY[:],
}
sio.savemat(fashion_mnist, mdict) # 保存到本地 fashion_mnist.mat