Is it possible to display a MATLAB plot in python?

48 Ansichten (letzte 30 Tage)
Oemer Aran
Oemer Aran am 9 Apr. 2018
Bearbeitet: Bardo am 28 Sep. 2021
I found one example with the Compiler SDK for Java here, but none for a python application.
I tried to package the following code to a python module via the Librabry Compiler (Compiler SDK):
function drawplot(x,y)
plot(x,y);
I installed the module and then tried to call the function in Python:
import PlotDrawer
myPlotter = PlotDrawer.initialize()
myPlotter = PlotDrawer.drawplot(5,5)
The import works, but I get this error:
AttributeError: module 'PlotDrawer' has no attribute 'drawplot'
When I don't use plots like in this example I don't get any errors and it works just fine. My guess is that MATLAB Library Compiler ignores functions with graphical functions in it. Is it even possible to display a MATLAB Plot in Python via the Compiler SDK?
I have the code written already in MATLAB and want to create a GUI in Python (for more flexibility and better, professional appearance). Any other approaches?
Thanks in Advance!

Akzeptierte Antwort

Kojiro Saito
Kojiro Saito am 16 Apr. 2018
Bearbeitet: Kojiro Saito am 16 Apr. 2018
There are two things I found in your codes.
  1. After initialization, you need to use the same variable name ( myPlotter) in your python script.
  2. The number of output from drawplot function is 0, so you need to specify nargout=0 in the function call.
Here's the Python code which will display MATLAB figure.
import PlotDrawer
myPlotter = PlotDrawer.initialize()
myPlotter.drawplot(5, 5, nargout=0)
Also, if want to input arrays, matlab.double is available by importing matlab.
The following python code will dipslay 2D line plot.
import PlotDrawer
import matlab
myPlotter = PlotDrawer.initialize()
x = matlab.double([1,2,3,4,5])
y = matlab.double([1,2,3,4,5])
myPlotter.drawplot(x, y, nargout=0)
Also, there's a user's guide for MATLAB Compiler SDK for Python, which might help you.
  4 Kommentare
Kushal Malhotra
Kushal Malhotra am 27 Jul. 2021
Hey @Tianya Duan, you could try this
eng.hold("on",nargout=0)
Initialize this before you initialize the plot. If this doesn't work then I would say, this phenomenon occur because the python program terminates. So if the above line doesn't work then sleep your python program for a while using this
time.sleep(1000)
Add it at the very end of your python program.
Bardo
Bardo am 28 Sep. 2021
Bearbeitet: Bardo am 28 Sep. 2021
Hi,
may I ask where to find the PlotDrawer() routine?
Thx

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Python Package Integration finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by