YAML 1.1 parser and emitter for MATLAB R2019b or newer. Based on SnakeYAML 1.30.
>> matlabData.a = [1.23; 4.56];
>> matlabData.b = {int32(2), {true, "hello", []}};
>> yamlString = yaml.dump(matlabData)
"a: [1.23, 4.56]
b:
- 2
- [true, hello, null]
"
>> matlabData = yaml.load(yamlString)
a: {[1.2300]; [4.5600]}
b: {[2]; {3×1 cell}}
>> yaml.dumpFile("test.yaml", matlabData)
>> matlabData = yaml.loadFile("test.yaml")
a: {[1.2300]; [4.5600]}
b: {[2]; {3×1 cell}}
>> yamlString = yaml.dump(matlabData, "auto") % default
"a: [1.23, 4.56]
b:
- 2
- [true, hello, null]
"
>> yamlString = yaml.dump(matlabData, "block")
"a:
- 1.23
- 4.56
b:
- 2
- - true
- hello
- null
"
>> yamlString = yaml.dump(matlabData, "flow")
"{a: [1.23, 4.56], b: [2, [true, hello, 'null']], c: [2, [true, hola]]}
"
YAML null
values are represented by empty Matlab arrays of any type with all sizes equal zero.
>> result = yaml.load("null")
[]
>> s = yaml.dump([])
"null
"
Use the ConvertToArray
option to convert uniform YAML sequences to MATLAB non-cell arrays.
>> yaml.load("[[1, 2], [3, 4]]")
2×1 cell array
{2×1 cell}
{2×1 cell}
>> yaml.load("[[1, 2], [3, 4]]", "ConvertToArray", true)
1 2
3 4
In MATLAB, there is no difference between a scalar and a one-element sequence. This can lead to unintended behaviour when using standard non-cell arrays since information about the data structure can be lost when loading and dumping data. For example, yaml.dump(1)
will write the YAML string "1.0"
even though you might have intended to write a sequence with one element "[1.0]"
.
To avoid these ambiguities
- convert all you array data to nested vector cells before dumping and
- load all data without the
ConvertToArray
option.
Thanks to the following people for their contributions: