Is it possible to display a MATLAB plot in python?

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!

 Accepted Answer

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 Comments

Thank you that worked. Kind of embarrassing that I overwrote the variable "myPlotter". The nargout=0 was really helpful tho.
Kojiro Saito, hi, when I use python call the funtion plot, the figure only exists few seconds, why does have this phenomenon occur? How could I change or add something? Thank you very much.
AddTset.m
function c = AddTest(a,b)
c = a + b;
figure(1)
plot(a,c,'DisplayName','test');
%pause(30);
end
pyCallMat.py
import matlab.engine
if __name__ == '__main__':
eng = matlab.engine.start_matlab()#可以为所欲为的调用matlab内置函数
a = eng.AddTest(1.0,2.0) #引用自写的脚本
print(a)
b = eng.sqrt(4.) #引用matlab内置函数
eng.figure(3.0)
eng.plot(a, b, nargout=0)
print(b)
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 on 28 Sep 2021
Edited: Bardo on 28 Sep 2021
Hi,
may I ask where to find the PlotDrawer() routine?
Thx

Sign in to comment.

More Answers (0)

Categories

Asked:

on 9 Apr 2018

Edited:

on 28 Sep 2021

Community Treasure Hunt

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

Start Hunting!