Main Content


Set environment variable



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

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

A process launched using the MATLAB® system, unix, dos, or ! function reads the values assigned to variables using the setenv function.

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


collapse all

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

Input Arguments

collapse all

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

The maximum number of characters in name is 215 – 2, or 32,766. 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 scalar or character vector.

Example: "C:\TEMP"

Extended Capabilities

Version History

Introduced before R2006a