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

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

MATLAB与Python版本的兼容

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

操作说明

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

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

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

pe = pyenv;
pe.Version

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

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)
% 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 supportedclc;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)

# -*- 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 2022The 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 linalgC1 = 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/

http://www.lryc.cn/news/141841.html

相关文章:

  • 【android12-linux-5.1】【ST芯片】驱动与HAL移植后数据方向异常
  • JavaScript Es6_3笔记
  • Qt产生随机数
  • postgresql常用函数-数学函数
  • 【排序】快速排序(前后指针法)—— 考的最少的一种算法
  • 软考:中级软件设计师:关系代数:中级软件设计师:关系代数,规范化理论函数依赖,它的价值和用途,键,范式,模式分解
  • openCV实战-系列教程2:阈值与平滑处理(图像阈值/图像平滑处理/高斯/中值滤波)、源码解读
  • C语言第五章-循环结构练习
  • Echarts面积图2.0(范围绘制)
  • flink checkpoint时exact-one模式和atleastone模式的区别
  • QEMU 仿真RISC-V freeRTOS 程序
  • 用大白话来讲讲多线程的知识架构
  • 【uniapp】微信小程序 , 海报轮播图弹窗,点击海报保存到本地,长按海报图片分享,收藏或保存
  • SpringBoot与前端交互遇到的一些问题
  • Maven介绍与配置+IDEA集成Maven+使用Maven命令
  • 毕业设计题目源码-毕业论文参考
  • SSH报错-Terminal shell path: C:\WINDOWS\System32\cmd.exe 此时不应有
  • Docker 轻量级可视化工具Portainer
  • 站点平台技术架构
  • 一个以太坊合约的漏洞分析-重入攻击
  • 测试先行:探索测试驱动开发的深层价值
  • 如何用Dockerfile部署LAMP架构
  • 基于量子粒子群算法(QPSO)优化LSTM的风电、负荷等时间序列预测算法(Matlab代码实现)
  • SQL Server软件安装包分享(附安装教程)
  • 基于Django的博客管理系统
  • windows系统依赖环境一键安装
  • centos7安装nacos
  • 【python】python智能停车场数据分析(代码+数据集)【独一无二】
  • 如何使用Redis来防止穿透、击穿和雪崩问题
  • 以getPositionList为例,查找接口函数定义及接口数据格式定义