Pyrunfile input from Matlab workspace

14 次查看(过去 30 天)
Hello,
I'm trying to get data via a python code using a dll. I already have access but only for specific files, running with
Outvars = pyrunfile(file input, outputs)
Now I want the filname that is used in python as an input parameter from matlab like:
fileName = 'C:/...';
Outvars = pyrunfile("file" fileName, outputs);
Is this somehow possible?
Another idea is not to use pyrunfile but creating a module in python and then calling the functions with py.my_function() but then I don't know how to initialize the database-access with the changing fileName. So I would still need to change the filename in the python module script.

回答(1 个)

Infinite_king
Infinite_king 2023-10-5
Hi,
I understand that you want to execute a Python script from MATLAB and pass a filename stored in the MATLAB workspace as an input parameter to the Python script.
You can achieve this by using ‘pyrunfile’ function. The first argument of ‘pyrunfile’ function should be a string containing python script name followed by input arguments. Refer the below code snippet,
% file name stored in MATLAB variable
file_name = "<path>";
% python file name
python_file_name = "python_prog.py";
% create a string with python script name followed by input arguments
str = python_file_name + " " + file_name;
% displaying the string
disp(str);
% calling python file by passing matlab variable as argument
pyrunfile(str);
The python script will receive these inputs as strings. For example let’s consider above example, the script ‘python_prog’ will receive ‘<path>’ as a string.
For more information on how to use ‘pyrunfile’ function, refer the following documentation.
Hope this is helpful.

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by