Pyrunfile input from Matlab workspace
13 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Infinite_king
on 5 Oct 2023
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.
0 Comments
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!