MATLAB Answers

How can I set a system-wide environment variable from within MATLAB?

86 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日
回答済み: Nelson 2014 年 7 月 1 日
I want to set an environment variable from within MATLAB. The value I set should be visible even for other processes on my system. If I use the "setenv" function in MATLAB, the variable is only visible in the current MATLAB process. The same thing happens if I use the DOS command "set":
>> !set my_var=some_value

採用された回答

MathWorks Support Team
MathWorks Support Team 2013 年 10 月 18 日
This is a limitation of the Windows operating system. Environment variables that are set from within a process, instead of being set through the control panel, are visible only to that process.
A possible workaround would be to use ActiveX functionality, as implemented in the attached file. Please note that setting a system-wide variable this way requires a restart in order for the changes to take effect.
As another possible workaround, Microsoft offers a tool called "setx" which allows you to set system-wide environment variables.This command is available in Windows versions later than Windows XP Service Pack 2 and Windows 2000. More information on the "setx" command can be found at:
Here is an example how to use it from within MATLAB:
>> !setx my_var some_value
This way of setting system-wide environmental variable does not require a restart of Windows or user re-logging on. However, note that if you use the Windows command window and
the "set" command to view the environmental variables, you will need to restart the command window to see the change.

その他の回答 (1 件)

Nelson
Nelson 2014 年 7 月 1 日
When using setx, I've noticed that the current Matlab session doesn't see the change when I used getenv. However, after restarting Matlab getenv pulls the updated value.
Is there a way to refresh Matlab without restarting it?

製品


リリース

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by