0

After building and installing the Python engine shipped with Matlab 2019b in Anaconda

(TestEnvironment) PS C:\Program Files\MATLAB\R2019b\extern\engines\python> C:\Users\USER\Anaconda3\envs\TestEnvironment\python.exe .\setup.py build -b C:\Users\USER\MATLAB\build_temp install

for Python 3.7 I wrote a simple script to test a couple of features I'm interested in:

import matlab.engine as ml_e

# Start Matlab engine
eng = ml_e.start_matlab()

# Load MAT file into engine. The result is a dictionary
mat_file = "samples/lena.mat"
lenaMat = eng.load("samples/lena.mat")

print("Variables found in \"" + mat_file + "\"")
for key in lenaMat.keys():
    print(key)

# print(lenaMat["lena512"])

# Use the variable from the MAT file to display it as an image
eng.imshow(lenaMat["lena512"], [])

I have a problem with imshow() (or any similar function that displays a figure in the Matlab GUI on the screen) namely that it shows quickly and then disappears, which - I guess - at least confirms that it is possible to use it. The only possibility to keep it on the screen is to add an infinite loop at the end:

while True:
  continue

For obvious reasons this is not a good solution. I am not looking for a conversion of Matlab data to NumPy or similar and displaying it using matplotlib or similar third party libraries (I am aware that SciPy can load MAT files for example). The reason is simple - I would like to use Matlab (including loading whole environments) and for debugging purposes I'd like to be able to show this and that result without having to go through loops and hoops of converting the data manually.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • I expect this happens because your script finishes execution and the interpreter quits. Have you tried waiting for a keypress after you do `imshow()`? It's basically the same as an infinite loop but you don't have to ctrl-c to get out of it and you can have more code after. https://stackoverflow.com/questions/983354/how-to-make-a-python-script-wait-for-a-pressed-key – Pranav Hosangadi Oct 27 '21 at 15:38
  • Ah, indeed. I will give that a shot. Totally forgot about it. Hopefully this will not affect the GUI. – rbaleksandar Oct 27 '21 at 17:20

0 Answers0