passing variable from matlab to python using pyrunfile

Hi all,
I've been looking to find an answer for the following question;
I have a variable in my workspace that I need to use in a python script, which for some reason is more challenging in than expected. I have been looking around the Mathlab documentation which unfortunately has not been helpfull for me.
Any help to solve this would be greatly appreciated!
Sample code which I simplified for this message.
% Matlab: This is the line that calls function.py using var01, var02 and var03.
apply_cnn_command = pyrunfile ("function.py '" + var01 + var02 + var03+"'")
% In Python I am using the current code:
import sys
import os
import matlab.engine
eng= matlab.engine.start_matlab()
varpy01 = eng.workspace[var01]
% The error message: "Python Error: NameError: name var01 is not defined.

Respuestas (1)

Pavan Sahith
Pavan Sahith el 12 de Mayo de 2024
Editada: Pavan Sahith el 12 de Mayo de 2024
Hello Janssen,
It seems you are looking for a way to pass variables from MATLAB to python.
You can refer to the following MATLAB Answers which will give you some guidance
Hope this information will help you in getting started.

3 comentarios

Hi Pavan,
Thanks for your email. Unfortunately both options do not work. From the documentation it has become clear to me that I cannot use the "matlab.engine.shareEngine". My code contains a loop that would require to reshare the Matlab session each time it runs. It would require that ML has to be restarted for each loop, which is not a workable solution.
I have adjusted my code along the reasoning in the second option (https://in.mathworks.com/matlabcentral/answers/1991358) (see below). Unfortunately this does not work either. The defined variables are not recognized in the python script.
% Matlab: This is the line that calls function.py using var01, var02 and var03.
apply_cnn_command = pyrunfile ("function.py '" + var01 + var02 + var03+"'")
% In Python I am using the current code:
import sys
import os
% import matlab.engine
% eng= matlab.engine.start_matlab()
% varpy01 = eng.workspace[var01]
...rest of the py script
% The error message remains the same: "Python Error: NameError: name var01 is not defined.
When I directly define these ML variables in Py code directly, the code has no problem to run. It suggests that the problem lies in how the ML variables are presented to the Py code. Unfortunately the ML documentation is very vague on this subject.
Walter Roberson
Walter Roberson el 12 de Mayo de 2024
Editada: Walter Roberson el 12 de Mayo de 2024
apply_cnn_command = pyrunfile ("function.py '" + var01 + var02 + var03+"'")
Do you have some kind of seperators built into var01 var02 var03 so that the parser can break apart the pieces?
I would have expected something closer to
apply_cnn_command = pyrunfile ("function.py '" + var01 + " " + var02 + " " + var03+"'")
Hi Walter,
Thanks for your response!
I litterally copied your suggestion and adjusted it for my code accordingly (except for the space at the end between "var03" and "+" (both options don't work). From the documentation I understand that the separator needs to be a space?
apply_cnn_command = pyrunfile ("function.py '" + var01 + " " + var02 + " " + var03 +"'")
Anyway, I think that this syntax is correct, since function.py starts up; yet as soon as it calls var01 in PY it crashes. This varialble is a character string describing a path to a folder, but with other variable types (integers and decimals), I have the same problems.
I cannot find any clear information how to present the different variable types to PY, and whether I need to preprocess these variables?
Thanks!
Barry

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2022a

Preguntada:

el 11 de Mayo de 2024

Comentada:

el 13 de Mayo de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by