필터 지우기
필터 지우기

Run a python exe from within a Matlab exe with Administrator permissions

조회 수: 16 (최근 30일)
Adithya Kalliath
Adithya Kalliath 2022년 10월 13일
편집: Gagan Agarwal 2023년 9월 10일
I would like to run a python.exe from within a Matlab.exe file with administrator permissions.Unfortunately, the python.exe does not have the necessary permissions to write out a file. I have tried executing it directly from command prompt as administrator and it works perfectly. The process fails when I try executing it within the matlab exe. The workflow is as follows:
  1. The python.exe receives two inputs as arguments i) filename - which is a path to a particular file ii) value
ML_correction.exe --filename "C:/.../../xx.txt" --corrected_val "-185.7"
The python exe computes a certain value and writes out a .mat file
2. The above command to call the python exe is executed within a Matlab exe as follows:
python_path="C:/.../../xx.txt";
val=-185.7;
cmd_command=['.\...\ML_correction.exe --filename "' python_path '" --corrected_val "' num2str(val) '"'];
[status,cmdout] = system(cmd_command);
I tried to pass my arguments using ShellExecute from Microsoft documentation but I am definitely doing it wrong. I have tried the following:
cmd_command=['.\...\ML_correction.exe --filename "' python_path '" --corrected_val "' num2str(val) '"'];
exepath=string(cmd_command);
uac = actxserver('Shell.Application');
uac.ShellExecute(exepath, "ELEV", "", "runas", 1);
Thank You!
I am using Windows 10 and Matlab2017b

답변 (1개)

Gagan Agarwal
Gagan Agarwal 2023년 9월 10일
편집: Gagan Agarwal 2023년 9월 10일
Hi Adithya Kalliath
To execute a Python script in MATLAB with admin privileges, you can utilize the 'system' function within MATLAB. This function allows you to invoke the Python interpreter with administrative rights.
Here are the steps you can follow to accomplish this:
  1. Open MATLAB with Administrative Privileges and navigate to the directory where your Python script is located.
  2. In MATLAB, create a new script or open an existing one.
  3. Use the system function to call the Python interpreter with administrative privileges. Consider the sample code below:
system('python "path_to_your_script.py"')
Replace "path_to_your_script.py" with the actual path of your Python script.
4. Now modify the file extension from '.m' to an executable file by executing the following command in the MATLAB command line.
mcc -m mfilename
Now, your Python file will be executed with administrative privileges.
For additional information regarding the ‘system’ function you can refer to the following documentation:

카테고리

Help CenterFile Exchange에서 Python Package Integration에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by