Passing variable from MatLab workspace as a keyword and value input argument of pyrunfile().
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2023-3-31
[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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!