matlab-dotenv

version 1.0 (37.7 KB) by Pavel Roslovets
Load environment variables from .env file

9 Downloads

Updated Tue, 15 Mar 2022 23:33:01 +0000

From GitHub

View License on GitHub

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 (2022). matlab-dotenv (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!

code

code/+dotenv

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.