简体   繁体   English

如何从 matlab 调用 python?

[英]How to call python from matlab?

Can I have both perl.m and python.m so that I can call either perl or python from matlab? Can I have both perl.m and python.m so that I can call either perl or python from matlab? Without knowing exactly how the original perl.m works, I do not feel comfortable simply to replace "perl" with "python".在不确切知道原始 perl.m 的工作原理的情况下,简单地将“perl”替换为“python”让我感到不舒服。 Please help.请帮忙。

If I understand you correctly, you have a file perl.m used to invoke Perl scripts from Matlab?如果我理解正确,您有一个文件perl.m用于从 Matlab 调用 Perl 脚本? Of course you can't just change its name to python.m and expect it to run Python!当然,您不能只将其名称更改为python.m并期望它运行 Python!

You have to look inside perl.m to see exactly what it does.必须查看perl.m内部才能确切了解它的作用。

Alternatively, Google on calling Python from Matlab - this SO discussion and many other useful resources immediately come up.或者,谷歌从 Matlab 调用 Python - 这个 SO 讨论和许多其他有用的资源立即出现。

You can use method:您可以使用方法:

status = dos(command)

where in 'command' You enter command like in terminal:在“命令”中的位置您可以在终端中输入命令:

for example:例如:

file - hw.py文件 - hw.py

print("Hello world")

matlab console: matlab控制台:

dos('python yourDir\hw.py')

output in terminal: output 在终端:

Hello world

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM