loadenv
Syntax
Description
loadenv(
loads environment
variables from filename
).env
and plain text files by parsing one key-value pair
per line and sets them as environment variables in the MATLAB® environment.
loadenv(
sets environment variables with additional options specified by one or more name-value
arguments.filename
,Name=Value
)
Examples
Load Environment Variable
Load the key-value pairs from a file to the MATLAB environment.
loadenv("samplefile.env")
Load Environment Variables into Dictionary
Load the key-value pairs from a file to a dictionary.
D = loadenv("samplefile.env")
Load Key-Values with File Type Adjustments
Create a file containing key-value pairs to use as environment variables.
keyvalues=["Key1='Value1'";"Key2='Value2'"]; writelines(keyvalues,"C:\Users\username\Desktop\samplefile.txt");
Load the key-value pairs to the MATLAB environment using a .env
file type.
loadenv("samplefile.txt",FileType="env")
Load Credentials from File to the MATLAB Environment
Create a .env
file containing a pair of AWS access
keys as key-value pairs.
keyvalues=["AWS_ACCESS_KEY_ID='my-aws-access-key-id-goes-here'";... "AWS_SECRET_ACCESS_KEY='my-aws-secret-access-key-goes-here'"]; writelines(keyvalues,"C:\Users\username\Desktop\s3secrets.env");
Load the key-value pairs from the file to the MATLAB environment.
loadenv("s3secrets.env")
View your environment variables.
getenv(["AWS_ACCESS_KEY_ID";"AWS_SECRET_ACCESS_KEY"])
ans = 2×1 string array "my-aws-access-key-id-goes-here" "my-aws-secret-access-key-goes-here"
Lookup Environment Variables from Dictionary
Load the key-value pairs from a .env
file into a
dictionary.
D = loadenv("secrets.env")
D = dictionary (string ⟼ string) with 11 entries: "AWS_ACCESS_KEY_ID" ⟼ "your-AWS_ACCESS_KEY_ID-goes-here" "AWS_SECRET_ACCESS_KEY" ⟼ "your-AWS_SECRET_ACCESS_KEY-goes-here" "MW_WASB_SAS_TOKEN" ⟼ "your-MW_WASB_SAS_TOKEN-goes-here" "username" ⟼ "your-username-goes-here" "password" ⟼ "your-password-goes-here" "DB_NAME" ⟼ "MySQL_DB" "DB_USER" ⟼ "user_name" "DB_PASSWORD" ⟼ "pass_word" "DB_DOMAIN" ⟼ "abc123.mysqldb.com" "DB_PORT" ⟼ "1234" "TEMPORARY_DOWNLOAD" ⟼ "C:/Users/username/Local/Temp/my_download"
Lookup a value in your dictionary.
D("DB_USER")
ans = "user_name"
Input Arguments
filename
— Name of the file to read
string scalar | character vector
Name of the file to read, specified as a string scalar or character vector. The file must contain the key-value pairs to set as variables using the key=value format, such as:
AWS_ACCESS_KEY_ID="my-aws-access-key-id-goes-here" AWS_SECRET_ACCESS_KEY="my-aws-secret-access-key-goes-here" username="your-username-goes-here"
.env
file format is the recommended plain text file format for
loadenv
. A .env
file (dotenv) is a plain text
file containing keys and their corresponding values that can be loaded into the MATLAB
environment. By using a .env
file you can separate sensitive
configuration data from code and provide different sets of configurations for different
workflows.
Depending on the location of your file, filename
can take on one
of these forms.
Location | Form | ||||||||
---|---|---|---|---|---|---|---|---|---|
Current folder or folder on the MATLAB path | Specify the name of the file in
Example:
| ||||||||
File in a folder | If the file is not in the current folder or in a folder on the
MATLAB path, then specify the full or relative path name in
Example:
Example:
| ||||||||
Internet URL | If the file is specified as an internet uniform resource locator
(URL), then Example:
| ||||||||
Remote Location | If the file is stored at a remote location, then
Based on the remote location,
For more information, see Work with Remote Data. Example:
|
While parsing the file, loadenv
will read one key-value pair per
line as well as:
Skip empty lines and treat all contents of a line after
#
as a comment.Interpret key names that consist only of letter, number, underscore, dot, and hyphen characters.
Preserve text in quotes without adjustment, trim whitespace outside of quotes, and expand new lines from double quoted values.
Read empty values as empty strings.
Note
Security Considerations: Because
.env
files are plain text, ensure the location and permissions of
your .env
file reflect the security level of your workflow:
Your local machine is often the most secure place to store your file.
During code deployment, do not deploy a
.env
file containing your credentials.Do not check your
.env
files into source code repositories. For example, in your repositories, include the.env
file extension in your.gitignore_global
file to exclude.env
files from all your repositories. For more information on how to configure Git to ignore files, see the GitHub page Ignoring Files.
Data Types: string
| char
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: loadenv(filename,OverwriteEnvironmentVariable=true)
overwrites existing environment variables.
OverwriteEnvironmentVariable
— Overwrite existing environment variables
true
or 1
(default) | false
or 0
Overwrite existing environment variables, specified as a numeric or logical
1
(true
) or 0
(false
).
If
true
,loadenv
will overwrite existing environment variables of the same name.If
false
,loadenv
will skip overwriting existing environment variables when reading the input file.
Example: OverwriteEnvironmentVariable=true
ExpandVariables
— Expand variables
true
or 1
(default) | false
or 0
Expand variables, specified as a numeric or logical 1
(true
) or 0
(false
). If
true
, loadenv
will apply variable expansion in
the form ${VAR}
where VAR
is the variable to be
replaced. If false
, loadenv
will not apply any
variable expansion.
When expanding variables, loadenv
seeks the variable value
first in the input .env
file, then the user environment. If the
variable value is not found in either location, a default value can be specified as
${VAR:-Default}
. If none of these apply,
loadenv
reads the variable value as empty.
Example: ExpandVariables=true
Encoding
— Character encoding scheme
"UTF-8"
(default) | "auto"
| "system"
| "ISO-8859-1"
| "windows-1251"
| "windows-1252"
| ...
Character encoding scheme associated with the file, specified as
"auto"
, "system"
, or a standard character
encoding scheme name.
If specified as
"auto"
,loadenv
will detect encoding from the file.If specified as
"system"
,loadenv
will use your system's default encoding.
Example: Encoding="UTF-8"
uses UTF-8
as the
encoding.
Example: Encoding="system"
uses the system default
encoding.
Data Types: char
| string
FileType
— Detect type of file
"auto"
(default) | "env"
Type of file, specified as one of these values:
"auto"
— Automatically detect the file format of the input file from the extension specified infilename
."env"
— Read the contents of the input file as.env
. If the file extension infilename
is not.env
, you can specify the value ofFileType
as"env"
to read the contents of the input file as a.env
file.
Example: FileType="env"
Output Arguments
D
— Output dictionary
dictionary of strings
Output dictionary, returned as a dictionary of strings. A dictionary is a map that
stores data as values, which can be indexed using corresponding
unique keys. For more information on dictionaries, see dictionary
.
Version History
Introduced in R2023a
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)