【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'>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python调用MATLAB函数有几种方法。 1. 使用MATLAB Engine API:MATLAB提供了Engine API,可以在Python中直接调用MATLAB函数。首先,确保你已经安装了MATLAB,并且已经将MATLAB引擎添加到Python的环境变量中。然后,可以使用以下代码示例调用MATLAB函数: ```python import matlab.engine # 启动MATLAB引擎 eng = matlab.engine.start_matlab() # 调用MATLAB函数 result = eng.my_matlab_function(arg1, arg2, ...) # 关闭MATLAB引擎 eng.quit() ``` 在上面的示例中,`my_matlab_function`是你想要调用MATLAB函数,`arg1`、`arg2`等是传递给函数的参数。 2. 使用MATLAB Compiler:如果你的MATLAB代码已经被编译为可执行文件或共享库,你可以使用`subprocess`模块在Python调用它。例如,如果你有一个名为`my_matlab_function.exe`的可执行文件,可以使用以下代码调用它: ```python import subprocess # 调用MATLAB可执行文件 result = subprocess.run(['my_matlab_function.exe', arg1, arg2, ...], capture_output=True, text=True) # 输出结果 print(result.stdout) ``` 在上面的示例中,`arg1`、`arg2`等是传递给MATLAB可执行文件的参数。 3. 使用MATLAB引擎for Python:MathWorks还为Python提供了MATLAB引擎for Python,它允许你在Python中直接调用MATLAB函数,类似于MATLAB Engine API。你可以通过安装`matlab`包来使用它。具体使用方法可以参考MathWorks官方文档。 无论使用哪种方法,你都需要确保已经正确安装了MATLAB,并且将其配置为可以与Python交互

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值