%% yaml Examples
% Table of Contents
% Dump
% Load
% Write to file
% Load from file
% Null value
% Load YAML sequences as MATLAB standard arrays
% Control dumping behaviour for MATLAB arrays

data.a = [1.23, 4.56];
data.b = "hello";
data.c = {int32(2), {true, 'hola'}};

%% Dump
% Convert data to a YAML string

s = yaml.dump(data)
yaml.dump(data, "block")
yaml.dump(data, "flow")

%% Load
% Convert a YAML string to MATLAB types

yaml.load(s)

%% Write to file

yaml.dumpFile("test.yaml", data)

%% Load from file

yaml.loadFile("test.yaml")
delete("test.yaml")

%% Null value
% The yaml.Null class is used to represent the YAML null value.

yaml.dump(yaml.Null)
yaml.dump(yaml.Null(2, 1))
yaml.load("null")

% You can use the yaml.isNull function to check if loaded values are null

yaml.isNull(yaml.load("123"))
yaml.isNull(yaml.load(".nan"))
yaml.isNull(yaml.load("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. You can use the ConvertToArray option to convert YAML sequences to 1D or 2D standard arrays. Beware however, that a cell is returned if a sequence cannot be converted to a standard array which can cause errors in subsequent code if not handled properly.

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

%% 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 scalar → YAML scalar (default) or YAML one-element sequence
% MATLAB vector → YAML 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})
yaml.dump({{1, 2}})