Filter löschen
Filter löschen

Can MATLAB run a Python command using an existing connected Python session?

1 Ansicht (letzte 30 Tage)
I'm using MATLAB 2022b linux, Python 3.10, Julia 1.8.1 on Fedora Workstation 36, 64-bit.
I'm trying to get the PyJulia Python package to work properly so that my collaborators can use my Julia packages from MATLAB. I made tutorial Python code that works very well in standalone Python, but I ran into problems when doing it through MATLAB's pyrunfile() and pyrun(). I researched this problem for a few days and suspect that it might has something to do with MATLAB loading certain shared libraries that have older versions, or perhaps PyJulia violated their limitations concerning local variables. I tried workaround like renaming pre-packaged library files and other tips with no success, so I'm exploring options involving shared MATLAB engines.
Suppose I launch a MATLAB desktop GUI from an existing Python session via the MATLAB engine API for Python, i.e. if I type the following code in the existing Python session, I have no errors, and a MATLAB desktop is launched.
from julia import Main # this is the PyJulia library. Load before matlab engine to avoid shared library conflicts.
import matlab.engine
eng = matlab.engine.start_matlab("-desktop")
# below: example code.
import numpy as np
y = np.array([1.1+2.1j, 3+4j, 5+6j])
Main.y = y # note this crashes if run via MATLAB's pyrun. No problem from standalone Python.
Main.eval('print("in julia: y is ", y)')
If I type pyenv in the launched MATLAB deskstop prompt, I see that no Python is loaded for use with pyrun(), system("python ...."), pyrunfile(), etc., as expected. Is it possible for me to run Python commands in the launched MATLAB desktop that uses the already connected existing Python session, instead of launching its own Python?
I understand that one can simply work from Python and call MATLAB via the engine API. However, I'm asking this question because my collaborators are unlikely to give up MATLAB's desktop environment (MATLAB language debugger, etc) and work in a Python development environment. Therefore, I'm looking for a way to control the connected existing Python session from MATLAB.
I welcome alternative work-around ideas to my problem too!

Antworten (1)

Naman Kaushik
Naman Kaushik am 3 Jul. 2023
Hi Roy!
I understand from your question that you wish to use your python modules from the MATLAB environment. I also understand that you have already explored the "pyrun()" function but "pyenv" returned a blank object.
I would recommend that you first check that your python path has been added to your environment variables so that MATLAB is able to detect it. This should resolve the issue.
I would also recommed
- Writing the your python script inside a function
- Place the python file in the same folder as the MATLAB script.
- Calling the function in MATLAB through "py.<file_name>.<function_name>"
You can check compatibilies through the following link:
To know mode about calling python functions from MATLAB:

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by