MATLAB與Python版本的兼容
具體可參看MATLAB與Python版本的兼容

操作說明
操作說明請參看下面兩個鏈接:
-
操作指南
簡單說明:
我安裝的是MATLAB2022a和Python3.8.6(安裝時請勾選所有可以勾選的,包括路徑)。對應(yīng)版本安裝完成后,在MATLAB命令行中敲入執(zhí)行路勁’D:\SoftwareApps\Python3_7\python.exe’(因人而異)
pyenv('Version','D:\SoftwareApps\Python3_7\python.exe')
完了以后執(zhí)行下面的操作:
pe = pyenv;
pe.Version
完成后的界面為:
因?yàn)镸ATLAB更多的是矩陣計(jì)算,我們需要安裝兩個庫,一個是numpy,一個是scipy。具體地,我們可以在windows 的操作終端(cmd/terminal)輸入python,進(jìn)入到python可以執(zhí)行的頁面,執(zhí)行以下命令:
import sys
import subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install','numpy'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install','scipy'])
完成之后,我們即可開始測試
-
測試案例
(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 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)
# -*- 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文章來源:http://www.zghlxwxcb.cn/news/detail-677113.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.zghlxwxcb.cn/news/detail-677113.html
到了這里,關(guān)于Windows下MATLAB調(diào)用Python函數(shù)操作說明的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!