필터 지우기
필터 지우기

Pyrunfile input from Matlab workspace

조회 수: 18 (최근 30일)
JuDi
JuDi 2022년 5월 20일
답변: Infinite_king 2023년 10월 5일
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