MartinKoch123 / yaml

MATLAB YAML parser and emitter based on SnakeYAML
MIT License
22 stars 7 forks source link

yaml

YAML 1.1 parser and emitter for MATLAB R2019b or newer. Based on SnakeYAML 1.30.

View yaml on File Exchange

Examples

Load and dump

>> data.a = [1.23, 4.56];
>> data.b = {int32(2), {true, "hello", yaml.Null}};

>> s = yaml.dump(data)
    "a: [1.23, 4.56]
     b:
     - 2
     - [true, hello, null]
     "

>> result = yaml.load(s)
    a: {[1.2300]  [4.5600]}
    b: {[2]  {1×3 cell}}

Read and write files

>> yaml.dumpFile("test.yaml", data)
>> result = yaml.loadFile("test.yaml")
    a: {[1.2300]  [4.5600]}
    b: {[2]  {1×3 cell}}

Styles

>> s = yaml.dump(data, "auto")  % default
    "a: [1.23, 4.56]
     b:
     - 2
     - [true, hello, null]
     "

>> s = yaml.dump(data, "block")
    "a: 
     - 1.23
     - 4.56
     b:
     - 2
     - - true
       - hello
       - null
     "

>> s = yaml.dump(data, "flow")
    "{a: [1.23, 4.56], b: [2, [true, hello, 'null']], c: [2, [true, hola]]}
     "

YAML null

>> result = yaml.load("null")
    Null

>> yaml.isNull(result)
   1

>> s = yaml.dump(yaml.Null)
    "null
     "

Load YAML sequences as MATLAB standard arrays

By default, sequences are loaded as nested cell arrays to distinguish between YAML scalars and YAML one-element sequences and to supported mixed type sequences. If you use the ConvertToArray option, sequences are converted to 1D or 2D standard arrays if possible:

>> yaml.load("[[1, 2], [3, 4]]", "ConvertToArray", true)
     1     2
     3     4

Control dumping behaviour for MATLAB arrays

Since every MATLAB scalar is always an array and every array technically has at least 2 dimensions, there exists two ambiguities when dumping arrays:

To avoid theses ambiguities and get consistent conversion behaviour, convert all your array data to nested vector cells before dumping them.

>> yaml.dump({1})
    "[1.0]
    "
>> yaml.dump({{1, 2}})
    "- [1.0, 2.0]
    "