readyaml

MATLAB function to read yaml files. Outputs result into struct.
37 Downloads
Updated 3 Feb 2024

readyaml

MATLAB function to read yaml files. Outputs result into struct.

View readyaml on File Exchange

Installation

clone this repository by typing in your command window:

git clone https://github.com/MaartenJongeneel/readyaml.git

Then, in MATLAB, you can add this folder and functions to your path via

addpath(genpath('path/to/readyaml'));

Usage

You can simple read out your yaml file in matlab via

yamlFile = 'path/to/my/file.yaml';
YamlStruct = readyaml(yamlFile);

Limitations

As keys will be written to MATLAB struct names, they name of the key should be compatible with what MATLAB can have as struct names. This means a key must begin with a letter, can contain letters, digits, or underscore characters, and are case sensitive and keys cannot contain periods.

test:
  1note: this is an invalid fieldname in MATLAB
  note1: this is a valid fieldname in MATLAB
  note.1: this is also invalid in MATLAB

As MATLAB cannot have a struct with duplicate fieldnames, it will automatically overwrite existing keys of the YAML file. As a result, in the example below, only the second note will be saved, as it will overwrite the first note.

test:
  note: this is a comment
  note: but this comment will overwrite the previous one, due to duplicate key names

Testing

We have provide a test.yml file, that that can be used to test out the function. It contains some comments, objects, lists, numerical arrays, and matrices to show what can be stored.

Posibilities

Current form of the yaml parcer supports the following:

  • Ordinary comments
    #ordinary comments can start anywhere on a line
  • Key value pairs
    key: value
  • Lower levels key value pairs
    person:
      name: John
      age: 34
  • Ordinary lists
    person:
      - John
      - Rae
  • Lists with nested objects
    persons:
      - name: John
        age: 34
      - name: Rae
        age: 4
  • Sequences of keys like
    person:
      - [name, age]: [Rae Smith, 4] 
      - [name, age]: [John, 34] 
  • Matrices (list of numerical arrays)
    matrix:
      - [0, 1, 2]
      - [3, 4, 5]
      - [6, 7, 8]
  • List with objects that contain list of objects etc.
    example:
      - martin:
          name: Martin D'vloper
          job: Developer
          skills:
            - python: good
            - perl: good
            - pascal: moderate
      - tabitha:
          name: Tabitha Bitumen
          job: Developer
          skills:
            - lisp: good
            - fortran: moderate
            - erlang: novice
  • Possibility of inline blocks like
    example:
      - {name: John Smith, age: 33}
      - {name: John Doe, age: 41}

To Do

In current form, the following components are still missing:

  • Multi-line strings using | and > characters
    data: |
      There once was > [] a tall man from Ealing
      Who got on a bus to Darjeeling
          It said on the door
          "Please don't sit on the floor"
      So he carefully sat on the ceiling
    data: >
      Wrapped text
      will be folded
      into a single
      paragraph
    
      Blank lines denote
      paragraph breaks
  • Node anchors (using &) and references (using *)
    - step:  &id001                  # defines anchor label &id001
        instrument:      Lasik 2000
        pulseEnergy:     5.4
        pulseDuration:   12
        repetition:      1000
        spotSize:        1mm
    
    - step: &id002
        instrument:      Lasik 2000
        pulseEnergy:     5.0
        pulseDuration:   10
        repetition:      500
        spotSize:        2mm
    - Instrument1: *id001   # refers to the first step (with anchor &id001)
    - Instrument2: *id002   # refers to the second step

Author

Maarten J. Jongeneel, Ph.D. researcher
Eindhoven University of Technology (TU/e), Mechanical Engineering Dept.
Contact: email

Cite As

Maarten Jongeneel (2024). readyaml (https://github.com/MaartenJongeneel/readyaml/releases/tag/v1.1.3), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2023b
Compatible with any release
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!
Version Published Release Notes
1.1.3

See release notes for this release on GitHub: https://github.com/MaartenJongeneel/readyaml/releases/tag/v1.1.3

1.1.1.0

See release notes for this release on GitHub: https://github.com/MaartenJongeneel/readyaml/releases/tag/v1.1.1

1.1.0.0

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

1.0.1.0

See release notes for this release on GitHub: https://github.com/MaartenJongeneel/readyaml/releases/tag/v1.0.1

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.