Get environment variable in MATLAB

In this article, we are going to discuss “Getting environment variable”, which can be done using getenv() function. The getenv() function is used to get the operating system environment variable.

Syntax:

value = getenv(name)

Parameters: This function accepts a parameter name parameter.

  • name: This is the specified name for which environmental variable is going to be searched.

Return Value: It returns the operating system environment variable.

Example 1

Matlab




% MATLAB code for Environment Variable GFG
% has been created and later replaced with "C:\GFG" variable
% Calling the setenv() function
% to replace the "GFG" name
% with value "C:\GFG"
setenv('GFG','C:\GFG');
 
% Calling the getenv() function to
% Get the current environment variable name
getenv('GFG')


 

 

Output

 

ans = C:\GFG

 

Here the getenv() function returns the current environmental variable name “C:\GFG”.

 

Example 2

 

Matlab




% MATLAB code for Calling the setenv()
% to set the value
% "D:\w3wiki\gfg" over
% the variable "a"
setenv('a','D:\w3wiki\gfg');
 
% Calling the getenv() function to
% Get the current environment variable name
getenv('a')


 

 

Output

 

ans = D:\w3wiki\gfg