How to set environment variables for current account that can be used by other applications?

21 次查看(过去 30 天)
In a matlab script, I am trying to edit the environment variable , namely "Path" for the current user.
Now, getenv("Path") gives all the Path variable available including the system-wide path variables but I am only trying to access the current user's Path environment variable.
So I opted for:
current_path = winqueryreg('HKEY_CURRENT_USER','Environment','Path');
This returns the Path Variable just for current user as a character array which I can easily manipulate/edit, remove and add the desired path to it.
edited_path = edit_path(current_path);
My problem is : I want to set this character array as the Path variable for the current user. I tried doing it with the system command.
system('setx Path =edited_path')
However, it simply returns 0 and does not work . I even tried just a basic thing like :
system('setx Path ="test"');
This did not seem to work either.
I also tried with the set command instead of setx because it is just the current session that needs the environment variable but that did not work either.
Now, I could opt for setenv but I am not sure if this would affect the environment variables for other tools/applications.
The idea is : the script would run another application through a bat file that needs to use the changed environment variable.
Is there any way to go about this?

采纳的回答

Abhishek
Abhishek 2023-7-3
Hi Biraj,
You can use the setx command along with the system function in MATLAB to set the environment variable "Path" for the current user. However, there are a few modifications you can try out to make it work.
Make sure to appropriately concatenate the edited_path variable with the system command string. Right now, rather than giving the variables value, you are passing the text edited_path.
In the system command, enclose the value of the edited_path variable in double quotations, this is necessary to handle paths with space and special characters.
% Get the current Path variable for the current user
current_path = winqueryreg('HKEY_CURRENT_USER', 'Environment', 'Path');
% Manipulate/edit the current_path variable as needed
edited_path = edit_path(current_path);
% Set the edited_path as the Path variable for the current user
system(['setx Path "', edited_path, '"']);
Further you can refer to the following documentation pages:
  1 个评论
Biraj Khanal
Biraj Khanal 2023-7-3
I used the setenv and tried running the bat file and apparently, setenv can modify the path for current session and as a result any application run in the session takes the edited path. That is how I made it work.
But thanks for your suggestion. I see why the system command was not working because I did not concatenate the texts well. Thanks a lot! :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by