Main Content

setenv

Set environment variable

Description

example

setenv(name,value)sets the value of an operating system environment variable. Ifnameexists as an environment variable, thensetenvreplaces its current value withvalue. Ifnamedoes not exist, thensetenvcreates an environment variable callednameand assignsvalueto it.

setenvpassesnameandvalueto the operating system unchanged. Special characters, such as;,/,:,$, and%, are unexpanded and intact invalue.

A process spawned using the MATLAB®system,unix,dos, or!function reads the values assigned to variables using thesetenvfunction. You can retrieve any value set withsetenvby callinggetenv(name).

setenv(name)assigns a null value toname. This syntax is equivalent tosetenv(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.

Examples

collapse all

setenv('TEMP','C:\TEMP'); getenv('TEMP')
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 innameis 215- 2 (or 32766). Ifnamecontains the=character, thensetenvthrows 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

Version History

Introduced before R2006a