Python调用matlab代码(将matlab程序打包成python package调用)
首先打开matlab,在matlab命令行里输入deploytool,选择Library Compiler,然后在左上角选择python package,然后再中间(粉色)那里选择你要打包的函数文件(点那个蓝色的加号),这个方法会把你选择的matlab函数文件打包成一个python库
你可以再下面设置库名(Library name)
函数:
yuxipy_matlab1.m
function [a,b,c] = yuxipy_matlab1(b,h)
a = b.* h
b = b.* 3
c = cat(2,a,b)
%plot(c)
end
生成:
cd D:\pythoncodes\pachong\yuxipy_matlab1\for_redistribution_files_only
python setup.py install
D:\>cd D:\pythoncodes\pachong\yuxipy_matlab1\for_redistribution_files_only
D:\pythoncodes\pachong\yuxipy_matlab1\for_redistribution_files_only>python setup.py install
D:\softer\Aanaconda\lib\site-packages\setuptools\dist.py:544: UserWarning: The version specified ('R2022a') is an invalid version, this may not work as expected with newer versions of setuptools, pip, and PyPI. Please see PEP 440 for more details.
warnings.warn(
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\yuxipy_matlab1
copying yuxipy_matlab1\__init__.py -> build\lib\yuxipy_matlab1
copying yuxipy_matlab1\yuxipy_matlab1.ctf -> build\lib\yuxipy_matlab1
running install_lib
creating D:\softer\Aanaconda\Lib\site-packages\yuxipy_matlab1
copying build\lib\yuxipy_matlab1\yuxipy_matlab1.ctf -> D:\softer\Aanaconda\Lib\site-packages\yuxipy_matlab1
copying build\lib\yuxipy_matlab1\__init__.py -> D:\softer\Aanaconda\Lib\site-packages\yuxipy_matlab1
byte-compiling D:\softer\Aanaconda\Lib\site-packages\yuxipy_matlab1\__init__.py to __init__.cpython-39.pyc
running install_egg_info
D:\softer\Aanaconda\lib\site-packages\pkg_resources\__init__.py:123: PkgResourcesDeprecationWarning: R2022a is an invalid version and will not be supported in a future release
warnings.warn(
running egg_info
creating matlabruntimeforpython.egg-info
writing matlabruntimeforpython.egg-info\PKG-INFO
writing dependency_links to matlabruntimeforpython.egg-info\dependency_links.txt
writing top-level names to matlabruntimeforpython.egg-info\top_level.txt
writing manifest file 'matlabruntimeforpython.egg-info\SOURCES.txt'
reading manifest file 'matlabruntimeforpython.egg-info\SOURCES.txt'
writing manifest file 'matlabruntimeforpython.egg-info\SOURCES.txt'
removing 'D:\softer\Aanaconda\Lib\site-packages\matlabruntimeforpython-R2022a-py3.9.egg-info' (and everything under it)
Copying matlabruntimeforpython.egg-info to D:\softer\Aanaconda\Lib\site-packages\matlabruntimeforpython-R2022a-py3.9.egg-info
removing 'build\lib' (and everything under it)
'build\bdist.win-amd64' does not exist -- can't clean it
'build\scripts-3.9' does not exist -- can't clean it
removing 'build'
D:\pythoncodes\pachong\yuxipy_matlab1\for_redistribution_files_only>
python调用
#python调用matlab
#绘图包导入
import matplotlib.pyplot as plt
#解决中文显示问题
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
#numpy
import numpy as np
#import matlab引擎
import yuxipy_matlab1 as matlab1
ccc=matlab1.initialize()
[f, g, k]=ccc.yuxipy_matlab1(3,4,nargout=3)#函数名称 yuxipy_matlab1
print('f',f,type(f),np.size(f))
print('g',g,type(g))
print('k',k,type(k))
结果:
f 12 <class 'int'> 1
g 9 <class 'int'>
k [[12,9]] <class 'matlab.int64'>