【Python调用matlab代码,函数等,交互-----笔记二】

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'>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值