start the python engine via a script
Answers (2)
0 votes
The issue here is that when the Python script ends, Python will automatically close the newly created MATLAB Engine. Hence you will not be able to access it.
In order to avoid that you can open a MATLAB instance and type the following command in the MATLAB command window:
>> matlab.engine.shareEngine('my_engine')
This shares the current MATLAB Engine and names it 'my_engine' so that you can connect to it from your jupyter notebook. You can also create a Python script to do this as follows:
import os
os.system('nohup matlab -minimize -r matlab.engine.shareEngine("my_engine")')
Once you have started the MATLAB Engine, you can use it in your code. Here is a simple example on how to do it.
import matlab.engine
eng = matlab.engine.connect_matlab('my_engine')
eng.sqrt(4.0) # You can put your code here
This will return the Python object for the MATLAB Engine named 'my_engine' and assign it to 'eng'. You can then execute whatever MATLAB code you want using the 'eng' Python object.
After you are done you can call 'eng.quit()' or simply close the MATLAB window to stop the MATLAB Engine.
Hope this helps.
2 Comments
0 votes
Categories
Find more on Call MATLAB from Python in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!