dotenv-matlab

Load environment variables from .env file
47 Downloads
Updated 15 Mar 2022

matlab-dotenv

Load environment variables from .env file

Description

Use .env files to store sensitive parameters like passwords, tokens, API keys etc. This tool allows you to automatically load all environment variables from .env file stored in current folder.

Installation

Approach 1 (easy and fast)

Execute in MATLAB:

eval(webread('https://raw.githubusercontent.com/roslovets/matlab-dotenv/main/installer/installweb.m'))

Approach 2 (manual)

Use Add-Ons Explorer in MATLAB to find and install matlab-dotenv.

Approach 3 (offline)

Download matlab-dotenv.mltbx from the latest release and open it in MATLAB.

Load environment variables

Use dotenv.load function to load environment variables from .env file.

Syntax

dotenv.load

dotenv.load(dotEnvFilePath)

Description

dotenv.load loads all environment variables from .env stored in current folder. Access loaded variables with getenv(___)

dotenv.load(dotEnvFilePath) allows you to specify absolute or relative path to the text file with env variables

Input arguments

  • dotEnvFilePath - absolute or relative path to the text file with env variables (.env). Scalar string | char array

Read environment variables

Use dotenv.read function to read environment variables from .env file to structure.

Syntax

env = dotenv.read

env = dotenv.read(dotEnvFilePath)

Description

env = dotenv.read reads all environment variables from .env stored in current folder into structure

env = dotenv.read(dotEnvFilePath) allows you to specify absolute or relative path to the text file with env variables

Input arguments

  • dotEnvFilePath - absolute or relative path to the text file with env variables (.env). Scalar string | char array

Output arguments

  • env - structure with values of variables. Scalar struct

Example

In current folder create file named .env file with content:

var1=text_value
mypass=securely_stored_password

Run this command in your script to load variables from created file:

dotenv.load

Access variables with built-in MATLAB function:

mypass = getenv("mypass")

If you use Git in your project add .env file to .gitignore.


Generated from GettingStarted.mlx with Live Script to Markdown Converter

Cite As

Pavel Roslovets (2024). dotenv-matlab (https://github.com/roslovets/matlab-dotenv/releases/tag/v1.0), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2022a
Compatible with R2016b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

dev

installer

main/code

main/code/+dotenv

Version Published Release Notes
1.0

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.