我正在尝试从 python 运行 MATLAB 函数。我正在关注 MathWorks Tutorial .在该教程中,他们指定可以在 python 中查看 MATLAB 脚本的输出。他们给出的例子有如下代码
% This code is in a MATLAB script called triarea.m
b = 5;
h = 3;
a = 0.5*(b.* h) % Notice that there is no semicolon to suppress output.
python 部分:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.triarea(nargout=0)
python脚本应该打印
a =
7.5000
当它作为普通的 python 脚本运行时(例如,使用 PyCharm),这非常有效。但是,当使用 Jupyter notebook 运行此 python 代码时,不会打印任何内容。
如何在使用 Jupyter notebook 运行 python 代码时获得正确的输出?
到目前为止,我已尝试指定标准输出(如 here 中所建议)。即我的 python 代码现在读取
import io
out = io.StringIO()
eng.triareaf(nargout=0, stdout=out)
但是,我仍然无法获得正确的输出。我正在使用 Python 3.5、MATLAB R2017a、Jupyter 4.4.0 版和 Windows 10。
最佳答案
如果您仍然打开终端(即运行 Jupyter 服务器的终端),您会在那里找到您期望的输出。
顺便说一句,这不是解决方案,而只是一种变通方法……仍在寻找使终端输出直接出现在笔记本中的方法。
关于python - 使用 Jupyter Notebook 查看从 Python 运行的 MATLAB 代码的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50497374/