setenv - Set environment variable - MATLAB (original) (raw)
Syntax
Description
setenv([varname](#d126e1644555),[varvalue](#d126e1644595))
sets the values of operating system environment variables. Ifvarname
exists as an environment variable, thensetenv
replaces its current value withvarvalue
. If varname
does not exist, then setenv
creates an environment variable namedvarname
and assigns varvalue
to it.
setenv
passes varname
andvarvalue
to the operating system unchanged. Special characters, such as ;
, /
,:
, $
, and %
, are unexpanded in varvalue
.
A process launched using the MATLAB® system
, unix
, dos
, or!
function reads the values assigned to variables using thesetenv
function.
setenv([varname](#d126e1644555))
assigns a null value tovarname
. This syntax is equivalent tosetenv(varname,"")
. On most UNIX® platforms, an environment variable can exist with an empty value (""
). On the Microsoft® Windows® platform, this syntax is equivalent to removing the variable.
setenv([d](#mw%5Fcd7a3cdd-405b-4234-95a7-262032effdf3))
assigns all dictionary values to their associated environment variable names. (since R2023a)
Examples
Create Environment Variable
setenv("TEMP","C:\TEMP"); getenv("TEMP")
Append Folder to System Path
setenv("PATH",getenv("PATH") + ";D:\myfolder");
Create and Test Multiple Environment Variables
Create multiple environment variables, and then check that they exist.
setenv(["Var1" "Var2" "Var3" "Var4"],["Val1" "Val2" "Val3" "Val4"]); isenv(["Var1" "Var2"; "Var3" "Var4"])
ans = 2x2 logical array
1 1 1 1
Remove two of the environment variables using unsetenv
, and then check that they no longer exist.
unsetenv(["Var1" "Var4"]); isenv(["Var1" "Var2"; "Var3" "Var4"])
ans = 2x2 logical array
0 1 1 0
You can also remove environment variables using setenv
with missing
. Change the value of one environment variable and remove another.
setenv(["Var2" "Var3"],["ValB" missing]); isenv(["Var1" "Var2"; "Var3" "Var4"])
ans = 2x2 logical array
0 1 0 0
Input Arguments
varname
— Environment variable names
string array | character vector | cell array of character vectors
Environment variable names, specified as a string array, character vector, or cell array of character vectors.
The maximum number of characters in varname
is 215 – 2, or 32,766. Ifvarname
contains the =
character, then setenv
throws an error. The behavior of environment variables with =
in the name is not well defined.
Example: "PATH"
varvalue
— Environment variable values
string array | character vector | cell array of character vectors | missing
Environment variable values, specified as a string array, character vector, cell array of character vectors, or missing
. Remove an environment variable by setting its value tomissing
.
Example: "C:\TEMP"
d
— Environment variable names and values
dictionary
Environment variable names and values, specified as a dictionary. The specified dictionary can contain string arrays or cell arrays of character vectors.
Example: dictionary(["varname1","varname2"],["varvalue1","varvalue2"])
Example: dictionary({'varname1','varname2'},{'varvalue1','varvalue2'})
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
- Code generation does not support the use of
setenv
inside parfor loops.
Version History
Introduced before R2006a
R2023a: Set values of multiple environment variables
Set values of multiple environment variables by using setenv
with a string array or cell array of character vectors as input.
R2023a: Remove environment variables using missing
Remove an environment variable by setting its value tomissing
.