如何在Python中运行MATLAB代码 [英] How to run MATLAB code from within Python

查看:1297
本文介绍了如何在Python中运行MATLAB代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python运行MATLAB代码(我正在使用python 3.6).

I am trying to run a MATLAB code using Python (I'm using python 3.6).

我不需要传递任何参数或获取任何输出.我只需要在Python上运行一行代码即可直接运行MATLAB代码.

I don't need to pass any arguments or get any outputs. I just need a line of code on Python that will simply run the MATLAB code.

我在网上看到了一些答案,说要使用matlabroot并在命令提示符下使用它来安装某种引擎,但是它说我无法安装它,因为我的Python版本不够旧(这使得感.)

I saw some answers online that say to use matlabroot and to use that in the command prompt to install some sort of engine but it said I couldn't install it because my Python version was not old enough (which makes no sense).

是否有更简单的版本或执行此操作的另一种方式?

Is there an easier version or just another way to do this?

谢谢!

推荐答案

使用Oct2Py

您的第一个选择是使用Oct2Py,它与 Octave 一起运行可以运行Matlab文件和功能.只需使用以下终端命令安装它即可:

Using Oct2Py

Your first option is using Oct2Py which runs with Octave, a free and opensource Program that can run Matlab files and functions. Just install it with the following Terminal command:

pip3 install oct2py

然后,您可以像这样从Python脚本运行MatLab代码:

Then you can run MatLab Code from your Python script like that:

from oct2py import Oct2Py
oc = Oct2Py()


script = "function y = myScript(x)\n" \
         "    y = x-5" \
         "end"

with open("myScript.m","w+") as f:
    f.write(script)

oc.myScript(7)


使用MatLab

如果要使用原始的MatLab引擎,则必须执行以下步骤:


Using MatLab

If you want to use the original MatLab engine you would have to follow the following steps:

1.安装MatLab库

按照此页面的说明进行操作首先,必须打开MatLab并运行命令matlabroot,找到您的MatLab根文件夹.这应该给您Matlab的根文件夹.

Following the instructions of this page you first have to find your MatLab root folder by opening MatLab and running the command matlabroot. This should give you the root folder for Matlab.

然后打开终端(如果使用Windows,则可以按Windows + R,然后键入cmd并按Enter.).在终端中,运行以下代码:

Then you open your terminal (if you are using Windows you can do that by pressing Windows + R, then type cmd and press Enter.) In the terminal you run following code:

cd matlabroot\extern\engines\python

确保用刚刚找到的路径替换matlabroot.然后运行

Make sure to replace matlabroot with the Path you just found. Then you run

python3 setup.py install

要安装MatLab Python库.

To install the MatLab Python library.

2.使用MatLab库

按照此页面的说明然后可以

import matlab.engine

eng = matlab.engine.start_matlab()
tf = eng.isprime(37)
print(tf)

如果您要运行

If you want to run entire scripts, you can save your scripts as a MatLab *.m file in your current folder and run them like this:

import matlab.engine
eng = matlab.engine.start_matlab()
eng.myMatlabFile(nargout=0)

您还可以从Python创建MatLab文件:

You could also create the MatLab File from Python:

import matlab.engine

script = "b = 5;\n" \
         "h = 3;\n" \
         "a = 0.5*(b.* h)"

with open("myScript.m","w+") as f:
    f.write(script)

eng = matlab.engine.start_matlab()
eng.myScript(nargout=0)

我希望这会有所帮助:)

I hope this helps :)

这篇关于如何在Python中运行MATLAB代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆