Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewing Output of MATLAB Code run from Python Using Jupyter Notebook

I am attempting to run MATLAB functions from python. I am following the MathWorks Tutorial. In that tutorial they specify that output from a MATLAB script can be viewed in python. The example they give has the following code

% 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.

The python portion:

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

The python script is supposed to print

a =

    7.5000

This works perfectly when running it as a normal python script (for example, using PyCharm). However, nothing is printed when this python code is run using Jupyter notebook.

How does one get the correct output when running the python code using Jupyter notebook?

So far, I have tried specifying standard out (as suggested in here). Namely my python code now reads

import io
out = io.StringIO()
eng.triareaf(nargout=0, stdout=out)

However, I still am not able to get the correct output. I am using Python 3.5, MATLAB R2017a, Jupyter version 4.4.0 and Windows 10.

like image 798
sdrinz23 Avatar asked Feb 06 '26 02:02

sdrinz23


1 Answers

If you still have the terminal open (i.e. the one in which the Jupyter Server is running) you will find there the output you are expecting.

Btw, this is not a solution but just a workaround ... still looking for a way to make the terminal output appear directly in the notebook.

like image 91
mscipio Avatar answered Feb 09 '26 07:02

mscipio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!