a simple Matlab config parser for ASCII based files


Updated 9 Jun 2018

From GitHub

View License on GitHub

A simle config parser for types of string,boolean, vector,cell and matrix values. The parser uses simple headers with customizable header and comment delimiters. In other words use a delimiter of your preference for comments ( e.g. // or %, or ) and setting headers (e.g. {myoption1} or [myoption2]). De delimiters can be provided to readConfig as second and third input for comments and headers respectively. First option is the path to the config file.
The parser will generate a stuct with settings that are accecible via same header names. Example:
in config file --> set option with
1 2 3
in matlab file --> acces vector [1 2 3] opt struct with
The parser will automatically recognize the following types:
- (single) path/string = string with pathname/caracter
- (multiple) path/ = 1xm cell with m pathnames listed after {header}
- boolean = false or true logical (case sensitive!)
- vector = simple 1xm vector with m space separated values after header
- cell/matrix = 2D matrix with m columns (space separated values) and n rows (lines after header).
Note that the function returns a cell. To convert to array simply do cell2mat(opt.mycellarray)
File type is also customizable (i.e. txt or conf or whatever)
NOTE: do not put headers on the first line of the config file, this is intended for a short description like so: // name_of_config

order of headers is not relevant
create a txt file similar to config.txt e.g.
put desired header names in a cell with strings (order is not relevant but caracters must match)
acces config with:
[options] = readConfig('path/to/config',headernames,'//','{}',false); % varargin: 1=commentdelim, 2=headerdelim 3: -
loadOptionsList is a local function to create the header names. If this is not desired simply use:
optionsList = { 'path' , 'paths' , 'boolean' , 'vector' , 'cell' };
Usage of 2.0 functionality:

- as of 2.0 the parser will recognize pwd command. so using:
{mydir} --> {mydir}
pwd --> path/to/location/calling/master/function
In other words if you call readConfig with mast.m located at ~/somedirectory/ the options.mydir = ~/somedirectory/

Parser will now also allow variable additions. In other words if you define a variable {mydir} you can use this to construct a new variable composed partially or fully of {mydir}. Example:

use {mydir} to definecomposite path variable


Now the {myotherdir} will point to a folder level higher than {mydir} (this is due to addition ../)
This can also be use for addition of any string variables. Note that the functionality doesn't work yet for numeric values. In case there is interest i might consider adding that.

UPDATE 2.2.: for simulinkparser see my other submission -->
now the runtimesettings can be set using two methods:
1. via providing ordered variable arguments.
2. Via providing name tag of runtime option followed by the value e.q. readconfig(...,'structnamefieldfillelemn','__',...) irrespective of the order of variable argument this usage of readConfig will pick the right configuration for the explicit setting

example usage:

readConfig('config/config.txt', 'structnamefieldfillelemn', '___') is equavalent to readConfig('config/config.txt','//', '{}',true, '_', ...) (note how with the old method all preceding variable arguments must be provided to set the 'structnamefieldfillelemn' )
This particular option now replaces all caracters '/' and spaces in the header names with the desired fielname fill element '___'.

usage 3.0 --> update:
- parsing symbolic variables e.g. c2 = @ c1^2*2 (any expression allowed)
- parsing nested variables e.g. c3 = @ c1*2+10 (Parser evaluates
variables recursively such that nested relationships are allowed irrespective of ordering in the config file

example: --->

c1 = 0.01
k1 = 1.1
//using nested variables
c2 = @ 2844.4*c1+12

// also works for vars defined later in the file
n1 = @ c2*2

Cite As

Tigran Mkhoyan (2023). tmkhoyan/configParser (, GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2017b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: tmkhoyan/simulinkParser

Community Treasure Hunt

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

Start Hunting!

Versions that use the GitHub default branch cannot be downloaded

Version Published Release Notes

Bug fixes: now the order of how the nested variables are defines is irrelevant. Each expression is evaluated recursively until all expressions are defined (in the absence of errors)

UPDATE 3.0: allows to evaluate symbolic expressions such as a = b^2 +3 using syntax = @. The parser will sort and evaluate constant and expressions based on their recursive relationship i.e. See master.m under section update 3.0 for an example.

Bug fixes with delimiter between struct fieldnames. The example master.m is updated. See file for example of update 2.0 functionality usage
UPDATE for simulinkparser see my other submission -->
now the runtimesettings can be set by picking the desired setting vollowed by value. Much like plot(...'opt','value'...)

- bug fixes
- added config example 2.0 functionality

functionality update. Now the parser will recognize header variables at runtime and add them and allow simple string concatenation. Parser will also recognize pwd command. See explanation for detailed description.

updated readme
updated readme

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.