目录
1. matlab与python版本对应关系
matlab版本 | python版本 | matlab版本 | python版本 |
R2024b | 3.9, 3.10, 3.11, 3.12 | R2024a | 3.9, 3.10, 3.11 |
R2023b | 3.9, 3.10, 3.11 | R2023a | 3.8, 3.9, 3.10 |
R2022b | 2.7, 3.8, 3.9, 3.10 | R2022a | 2.7, 3.8, 3.9 |
R2021b | 2.7, 3.7, 3.8, 3.9 | R2021a | 2.7, 3.7, 3.8 |
R2020b | 2.7, 3.6, 3.7, 3.8 | R2020a | 2.7, 3.6, 3.7 |
R2019b | 2.7, 3.6, 3.7 | R2019a | 2.7, 3.5, 3.6, 3.7 |
R2018b | 2.7, 3.5, 3.6 | R2018a | 2.7, 3.5, 3.6 |
R2017b | 2.7, 3.4, 3.5, 3.6 | R2017a | 2.7, 3.4, 3.5 |
2. 检查matlab能否调用Python
3.内线程与外线程
Python解释器连接到MATLAB中有两种模式,一种是内线程(InProcess),一种是外线程(OutOfProcess)。当采用外线程模式时,能够使用terminate(pyenv)命令重启环境;当采用内线程模式时,重启环境必须重启MATLAB。
默认为内线程,如需要设置为外线程,使用如下代码:
pyenv(ExecutionMode="OutOfProcess")
4. 将Python解释器连接到MATLAB中
pyenv('Version','F:\anaconda3\envs\env_name\python.exe(Python解释器路径)')
如果需要使用的是anaconda中创建的环境中的包,需要连接到该环境下的python解释器,而不是base环境下的python解释器,如需设置线程模式,也可直接在连接matlab时设置。
5. matlab中python的基本使用
5.1 运行python代码及文件
如要在matlab中运行代码 l = [ 'A', 'new', 'list' ],可使用:
% 第一项为要运行的代码,第二项为返回的参数
myList = pyrun("l = ['A', 'new', 'list']", "l");
如运行一个python脚本mylist.py,脚本中包含代码 L = [ 'A', 'new', 'list' ]
myListFile = pyrunfile("mklist.py", "L")
5.2 调用自定义的python模块
(1)在matlab中添加python文件所在路径
% 解释器在搜索所需模块时会查找的目录列表
path = py.sys.path;
if count(path, 'python文件所在路径')
insert(path, int32(0), 'python文件所在路径')
end
(2)导入模块
obj=py.importlib.import_module('myfun(文件名称)')
(3)python文件有更新时,重新加载模块
py.importlib.reload(obj)
(4)卸载模块
clear classes
(5)实例:在python中写一个计算两个数相加的函数,在matlab中调用
a.创建python文件,编写加法函数
b. 导入模块
test = py.importlib.import_module('example')
c. 使用add函数
add函数需要传入a、b两个参数的值进行运算,可使用两种方法,一种是使用pyargs为python函数创建关键字参数,一种是使用name=value语法。
c = test.add(pyargs('a', 1, 'b', 2))
c = test.add(a=5, b=9)
5.3 数据类型转换
(1)生成list数据类型
data = py.list([1, 2, 3])
索引时,如果使用data[1],得到的数据类型为list;如果使用data{1},得到的数据类型默认为double,也可使用int()将其转为整型
data.append:可直接追加列表
class(data{1}):显示数值类型
data.len:显示数据长度
将数据转为double类型:
a. 将数据转为MATLAB元胞数组:cP=cell(P)
b. 将元胞数组转换为double类型:A = cellfun(@double, cP)
切片访问list:python中的格式为start:stop:step,MATLAB中的语法为start:step:stop
(2)数值类型转换
MATLAB输入数据类型 | 生成python数据类型 |
double single | float |
double single | complex |
int8、uint8、int16、uint16、int32 | int |
uint32、int64、uint64 | int |
NaN | float(“nan”) |
Inf | float(“inf”) |
string char | str |
string中的<missing>值 | None |
logical | bool |
dictionary | dict |
struct | dict |
table | py.pandas.DataFrame |
timetable | py.pandas.DataFrame |
datetime | py.datetime.datetime |
duration | py.datetime.timedelta |
(3)str类型
a. 在MATLAB中使用str变量:调用char()
b. 读取python字符串中的元素:和MATLAB字符串的索引方式相同
c. 传递MATLAB反斜杠控制字符:
调用MATLAB sprintf函数:
py.str(sprintf(‘The rain\nin Spain.’))
(4)字典dict
a. 创建python dict变量:
直接创建:
studentID = py.dict(Robert=357,Mary=229,Jack=391)
创建MATLAB结构体并进行转换:
S = struct(“Robert”,357,”Mary”,229,”Jack”,391)
studentID = py.dict(S)
b. 在MATLAB中使用python dict类型:
直接使用python dict:
order = py.dict(soup=3.54,bread=2.29,bacon=3.91,salad=5.00)
price = order{“bacon”}
将其转换为MATLAB变量:
myorder = struct(order)
price = myorder.bacon
索引键和值:keys(order)、values(order)
c. 将dict参数传递给python方法:
创建一个dict:
patient = py.dict(name="John Doe", ...
test1= [], ...
test2= [220.0, 210.0, 205.0], ...
test3= [180.0, 178.0, 177.5]);
使用update方法更新test1的值:
update(patient,py.dict(test1=[79.0, 75.0, 73.0]))