tmkhoyan/configPars​er

a simple Matlab config parser for ASCII based files
112 Downloads
Updated 9 Jun 2018

Description:
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
{myoption1}
1 2 3
in matlab file --> acces vector [1 2 3] opt struct with
opt.myoption1
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
Usage:
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:

define:
{mydir}
~/somepath/
use {mydir} to definecomposite path variable

{myotherdir}
{mydir}/../

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: --->

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

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

Cite As

Tigran Mkhoyan (2024). tmkhoyan/configParser (https://github.com/tmkhoyan/configParser), GitHub. Retrieved .

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

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
3.1.0.0

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)

3.0.0.0

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.

2.2.0.0

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'...)

2.1.0.0

- bug fixes
- added config example 2.0 functionality

2.0.0.0

UPDATE:
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.

1.0.0.0

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.