yaml

YAML parser and emitter for R2019b or newer
1.9K Downloads
Updated 20 Mar 2023

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:

  • MATLAB scalarYAML scalar (default) or YAML one-element sequence
  • MATLAB vectorYAML sequence (default) or YAML sequence containing one YAML sequence

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]
    "

Cite As

Martin Koch (2024). yaml (https://github.com/MartinKoch123/yaml/releases/tag/v1.5.4), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2021b
Compatible with R2019b and later releases
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!

+yaml

Version Published Release Notes
1.5.4.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.5.4

1.5.1

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.5.1

1.5.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.5.0

1.4.2

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.4.2

1.4.1

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.4.1

1.3.2

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.3.2

1.3.1

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.3.1

1.3.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.3.0

1.2.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.2.0

1.1.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/yaml/releases/tag/v1.1.0

1.0.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.