Python call matlab function 'plot', The figure disappeared in few seconds.

17 views (last 30 days)
When I use python call the matlab funtion plot, the figure only exists few seconds, then it colsed automatically. 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()
a = eng.AddTest(1.0,2.0)
print(a)
b = eng.sqrt(4.)
eng.figure(3.0)
eng.plot(a, b, nargout=0)
print(b)
  4 Comments
Tianya Duan
Tianya Duan on 26 Mar 2020
The question has been solved by Chinese matlab support team.
Two methods:
  1. add a line of the following command
input("Press Enter to continue...")
2. use the following answer of the website
isvalid function is more useful if you want to write a GUI.
while eng.isvalid(h):
pass
Villa Xue
Villa Xue on 11 Mar 2022
Thank you a lot, It works when use: input("Press Enter to continue...") in python

Sign in to comment.

Accepted Answer

Hornett
Hornett on 24 Jul 2024
The issue you're encountering is likely due to the fact that the MATLAB engine for Python does not keep the MATLAB session open once the Python script finishes executing. As a result, any figures created in MATLAB are closed when the session ends.
To keep the MATLAB figure open, you can use the matlab.engine's blocking parameter to keep the MATLAB session alive. Another approach is to add a pause in your MATLAB function to keep the figure open for a specified amount of time.
Solution 1: Using pause in MATLAB Function
You can add a pause statement in your MATLAB function to keep the figure open for a specified duration:
matlab
function c = AddTest(a, b)
c = a + b;
figure(1)
plot(a, c, 'DisplayName', 'test');
pause(10); % Pause for 10 seconds
end
Solution 2: Keeping MATLAB Session Alive
Modify your Python script to keep the MATLAB session alive using the blocking parameter:
python
import matlab.engine
if __name__ == '__main__':
eng = matlab.engine.start_matlab()
a = eng.AddTest(1.0, 2.0, nargout=1)
print(a)
b = eng.sqrt(4.0)
# Keep the MATLAB session alive
eng.set_param('0', 'SimulationCommand', 'start', nargout=0)
input("Press Enter to close MATLAB session...")
eng.quit()
Solution 3: Using matplotlib for Plotting in Python
If you don't need to plot in MATLAB specifically, you can use Python's matplotlib to plot the results. This way, you keep the plotting within the Python environment:
python
import matlab.engine
import matplotlib.pyplot as plt
if __name__ == '__main__':
eng = matlab.engine.start_matlab()
a = eng.AddTest(1.0, 2.0, nargout=1)
print(a)
b = eng.sqrt(4.0)
# Plot using matplotlib
plt.figure()
plt.plot([1.0], [a], label='test')
plt.xlabel('a')
plt.ylabel('c = a + b')
plt.title('Plot of a vs c')
plt.legend()
plt.grid(True)
plt.show()
print(b)
Summary
  • Solution 1: Add a pause statement in your MATLAB function to keep the figure open for a specified duration.
  • Solution 2: Modify your Python script to keep the MATLAB session alive using the blocking parameter.
  • Solution 3: Use Python's matplotlib for plotting instead of MATLAB.
Choose the solution that best fits your needs. If you still want to use MATLAB for plotting, Solution 1 or Solution 2 would be appropriate. If you are open to using Python for plotting, Solution 3 is a good option.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!