Passing variable from MatLab workspace as a keyword and value input argument of pyrunfile().
9 views (last 30 days)
Show older comments
Mr.Tentacles
on 30 Mar 2023
Commented: Mr.Tentacles
on 1 Apr 2023
I’m making an application in Matlab that will allow a user to input functions from Python using the pyrunfile() command. The application prompts the user to input the variable name that’s read into their Python script, the name of the output variables, and stores the data in a cell using the inputdlg() command. However im running into a problem with pyrunfile(). For example...
The following block of code returns the correct output:
pyFile = "TestingInputFunctionPython.py"; %name of python file being ran through matlab
varNamesOut = "dataOutPy"; %name of variable being output by python script
matlabDataOut = [1,2,3,4,5]; %some data in matlab
[pyData1] = pyrunfile(pyfile, [varNamesOut], pyDataIn = matlabDataOut)
The following block of code gives the error: Error using <string>><module>
Python Error: NameError: name 'dataInPy' is not defined
pyFile = "TestingInputFunctionPython.py"; %name of python file being ran through matlab
varNamesOut = "dataOutPy"; %name of variable being output by python script
varNamesIn = "dataInPy"; % name of variable inputted through python
matlabDataOut = [1,2,3,4,5]; %some data in matlab
[pyData1] = pyrunfile(pyFile, [varNamesOut], varNamesIn = matlabDataOut)
It seems as though the input variable name in python, in this case "dataInPy", must be inputted as plain text and NOT as a string or char (I have tried both). Is their any workaround for this or can i somehow convert the variable varNamesIn to return plain text rather then a data type of string or char?
0 Comments
Accepted Answer
Walter Roberson
on 31 Mar 2023
[pyData1] = pyrunfile(pyfile, [varNamesOut], pyDataIn = matlabDataOut)
MATLAB converts that into
[pyData1] = pyrunfile(pyfile, [varNamesOut], 'pyDataIn',matlabDataOut)
That is the word before the = is quoted and passed as a parameter and the expression after the = is evaluated and the results passed as another parameter
If you want the part before the = to be evaluated you need to use it without = and as a parameter of its own
varNamesIn, matlabDataOut
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB 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!