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 the key-value pairs from a file to the MATLAB environment.
loadenv("samplefile.env")Load the key-value pairs from a file to a dictionary.
D = loadenv("samplefile.env")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")
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"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
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
.envfile containing your credentials.Do not check your
.envfiles into source code repositories. For example, in your repositories, include the.envfile extension in your.gitignore_globalfile to exclude.envfiles 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.
Overwrite existing environment variables, specified as a numeric or logical
1 (true) or 0
(false).
If
true,loadenvwill overwrite existing environment variables of the same name.If
false,loadenvwill skip overwriting existing environment variables when reading the input file.
Example: OverwriteEnvironmentVariable=true
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
Character encoding scheme associated with the file, specified as
"auto", "system", or a standard character
encoding scheme name.
If specified as
"auto",loadenvwill detect encoding from the file.If specified as
"system",loadenvwill 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
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 infilenameis not.env, you can specify the value ofFileTypeas"env"to read the contents of the input file as a.envfile.
Example: FileType="env"
Output Arguments
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)