loadenv - Load environment variables from .env and plain text

  files - MATLAB ([original](https://www.mathworks.com/help/matlab/ref/loadenv.html)) ([raw](?raw))

Load environment variables from .env and plain text files

Since R2023a

Syntax

Description

loadenv([filename](#mw%5Ff86cdd9c-473b-4b5e-8ea4-5d3b7efc87a3)) loads environment variables from .env and plain text files by parsing one key-value pair per line and sets them as environment variables in the MATLAB® environment.

example

loadenv([filename](#mw%5Ff86cdd9c-473b-4b5e-8ea4-5d3b7efc87a3),[Name=Value](#namevaluepairarguments)) sets environment variables with additional options specified by one or more name-value arguments.

example

[D](#mw%5F22b90542-be7d-42df-828f-28544b27e9f6) = loadenv([filename](#mw%5Ff86cdd9c-473b-4b5e-8ea4-5d3b7efc87a3)) creates a dictionary from the key-value pairs. loadenv does not modify the MATLAB environment when an output argument is specified.

example

[D](#mw%5F22b90542-be7d-42df-828f-28544b27e9f6) = loadenv([filename](#mw%5Ff86cdd9c-473b-4b5e-8ea4-5d3b7efc87a3),[Name=Value](#namevaluepairarguments)) sets variables with additional options specified by one or more name-value arguments.

example

Examples

collapse all

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.

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.

Input Arguments

collapse all

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"

The.env file format is the recommended plain text file format forloadenv. 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 infilename.Example: "myFile.env"
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 infilename.Example: "C:\myFolder\myFile.env"Example: "dataDir\myFile.env"
Internet URL If the file is specified as an internet uniform resource locator (URL), then filename must contain the protocol type"http://" or"https://".Example: "http://hostname/path\_to\_file/my\_data.env"
Remote Location If the file is stored at a remote location, thenfilename must contain the full path of the file specified with the form:scheme_name://path_to_file/_my_file.ext_Based on the remote location,scheme_name can be one of the values in this table. Remote Locationscheme_nameAmazon S3™s3Windows Azure® Blob Storagewasb, wasbsHDFS™hdfsFor more information, see Work with Remote Data.Example: "s3://bucketname/path_to_file/my_file.env"

While parsing the file, loadenv will read one key-value pair per line as well as:

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:

Data Types: string | char

Name-Value Arguments

Specify optional pairs of arguments asName1=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 logical1 (true) or 0 (false).

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). Iftrue, 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.

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:

Example: FileType="env"

Output Arguments

collapse all

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