Clear Filters
Clear Filters

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

16 views (last 30 days)
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

Answers (1)

Gagan Agarwal
Gagan Agarwal on 10 Sep 2023
Edited: Gagan Agarwal on 10 Sep 2023
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:

Categories

Find more on Python Package Integration in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!