Windows下MATLAB调用Python函数操作说明

MATLAB与Python版本的兼容

具体可参看MATLAB与Python版本的兼容

操作说明

操作说明请参看下面两个链接:

  1. 操作指南
    简单说明:
    我安装的是MATLAB2022a和Python3.8.6(安装时请勾选所有可以勾选的,包括路径)。对应版本安装完成后,在MATLAB命令行中敲入执行路劲'D:\SoftwareApps\Python3_7\python.exe'(因人而异)
matlab 复制代码
pyenv('Version','D:\SoftwareApps\Python3_7\python.exe')

完了以后执行下面的操作:

matlab 复制代码
pe = pyenv;
pe.Version

完成后的界面为:

因为MATLAB更多的是矩阵计算,我们需要安装两个库,一个是numpy,一个是scipy。具体地,我们可以在windows 的操作终端(cmd/terminal)输入python,进入到python可以执行的页面,执行以下命令:

bash 复制代码
import sys
import subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install','numpy'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install','scipy'])

完成之后,我们即可开始测试

  1. 测试案例
    (1)MATLAB中的.m文件(call for Python)
matlab 复制代码
% use this link to check if your MATLAB version supports the Python version
% on your computer
% https://www.mathworks.com/support/requirements/python-compatibility.html
% if not, install the Python version that is supported

clc;clear all;

% define MATLAB matrices
A1=[1 2;
    3 4];
A2=[2 3;
    1 0];

% check the conversion types
% https://www.mathworks.com/help/matlab/matlab_external/passing-data-to-python.html
% convert matrices to Python objects
A1converted = py.numpy.array(A1);
A2converted = py.numpy.array(A2);
% parameter to be sent
parameter=py.int(2);

% sent the variables, call the python code in "test.py" and obtain the
% return list "result"
[result] = pyrunfile("haha.py","ReturnList",A=A1converted,B=A2converted,param1=parameter);
% link explaining pyrunfile() function:
% https://www.mathworks.com/help/matlab/ref/pyrunfile.html#mw_03ecec06-0677-4345-9112-ea93ac49881e

% check the data type of returned variables
class(result)
class(result{1})

% convert the Python arrays to MATLAB matrices
C1=double(result{1});
C2=double(result{2});
C3=double(result{3});
C4=double(result{4});

% compute the matrices in MATLAB to double check the results
C1check=A1+A2;
C2check=A1*A2;
C3check=eye(2,2);
C4check=inv(A1);

(2)Python中的.py文件(called by MATLAB)

python 复制代码
# -*- coding: utf-8 -*-
"""
Code that demonstrates how to run Python code from MATLAB
This is the Python code that is called from MATLAB
Author: Aleksandar Haber 
Date: December 2022

The matrices "A" and "B" are passed to Python from MATLAB
The list "ReturnList" is returned to MATLAB

"""

# You should run these code lines in a separate Python session if the libraries numpy and scipy are not installed
# import sys
# import subprocess
# subprocess.check_call([sys.executable, '-m', 'pip', 'install','numpy'])
# subprocess.check_call([sys.executable, '-m', 'pip', 'install','scipy'])

import numpy as np
from scipy import linalg

C1 = A + B
C2 = np.matmul(A,B)
C3 = np.eye(param1,param1)
C4 = linalg.inv(A)

# this list is returned to MATLAB and later on unpacked in MATLAB
ReturnList=[C1,C2,C3,C4]

具体到一些输入输出的语法,请参看链接:https://www.mathworks.com/help/matlab/ref/pyrunfile.html

参考

[1] https://www.mathworks.com/support/requirements/python-compatibility.html

[2] https://aleksandarhaber.com/tutorial-on-how-to-execute-python-code-directly-matlab/

[3] https://github.com/AleksandarHaber/Execute-Python-Code-From-MATLAB/blob/

相关推荐
IT猿手3 分钟前
最新高性能多目标优化算法:多目标麋鹿优化算法(MOEHO)求解TP1-TP10及工程应用---盘式制动器设计,提供完整MATLAB代码
开发语言·深度学习·算法·机器学习·matlab·多目标算法
__lost4 分钟前
MATLAB直接推导函数的导函数和积分形式(具体方法和用例)
数学·算法·matlab·微积分·高等数学
咸鱼桨20 分钟前
《庐山派从入门到...》PWM板载蜂鸣器
人工智能·windows·python·k230·庐山派
yusaisai大鱼35 分钟前
tensorflow_probability与tensorflow版本依赖关系
人工智能·python·tensorflow
Biomamba生信基地39 分钟前
R语言基础| 功效分析
开发语言·python·r语言·医药
CodeClimb1 小时前
【华为OD-E卷-木板 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
夜幕龙1 小时前
iDP3复现代码数据预处理全流程(二)——vis_dataset.py
人工智能·python·机器人
滚雪球~1 小时前
如何使用Windows快捷键在多显示器间移动窗口
windows·计算机外设
晚夜微雨问海棠呀2 小时前
长沙景区数据分析项目实现
开发语言·python·信息可视化
cdut_suye2 小时前
Linux工具使用指南:从apt管理、gcc编译到makefile构建与gdb调试
java·linux·运维·服务器·c++·人工智能·python