python - 使用 Jupyter Notebook 查看从 Python 运行的 MATLAB 代码的输出

标签 python python-3.x matlab jupyter-notebook jupyter

我正在尝试从 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/

相关文章:

xml - Python xml 安装不起作用

matlab - 向量转化为矩阵 Armadillo

matlab - 在 MatLab 中做导数

python - 从 Tensorflow 中的张量中删除一组张量

python - matplotlib:从数据集中删除数据时散点图不更新

python - 如何在Python中正确处理浮点运算?

c++ - 如果 Matlab 以 GUI 启动,如何从 MEX 文件中检查

python - 将python scikit学习模型导出到pmml

python - 使用pypy2.2时导入Numpypy

python - __init__() 需要 1 个位置参数,但 2 个给出了类型错误