Main Content


Set environment variable



setenv(name,value) sets the value of an operating system environment variable. If name exists as an environment variable, then setenv replaces its current value with value. If name does not exist, then setenv creates an environment variable called name and assigns value to it.

setenv passes name and value to the operating system unchanged. Special characters, such as ;, /, :, $, and %, are unexpanded and intact in value.

A process spawned using the MATLAB® system, unix, dos, or ! function reads the values assigned to variables using the setenv function. You can retrieve any value set with setenv by calling getenv(name).

setenv(name) assigns a null value to name. This syntax is equivalent to setenv(name,''). On the Microsoft® Windows® platform, this syntax is equivalent to undefining the variable. On most UNIX® platforms, it is possible to have an environment variable defined as empty.


collapse all

ans = 'C:\TEMP'
setenv('PATH', [getenv('PATH') ';D:\mypath']);

Input Arguments

collapse all

Environment variable name, specified as a string or a character vector.

The maximum number of characters in name is 215 - 2 (or 32766). If name contains the = character, then setenv throws an error. The behavior of environment variables with = in the name is not well-defined.

Example: 'PATH'

Environment variable value, specified as a string or a character vector.

Example: 'C:\TEMP'

Extended Capabilities

Introduced before R2006a