'Is there already a YAML library/parser for MATLAB?

I want to use YAML to communicate some data across multiple languages. (Think of it as "language-independent serialization".) One of these languages is MATLAB, but I can't seem to find a YAML library for that language. I've checked for "matlab yaml" and "matlab yaml parse" on Google and there don't seem to be any relevant results. A search of "yaml" on MATLAB Central also left me empty-handed.

Is there really no existing YAML library for MATLAB? I'm fine with writing one, but I would like to avoid duplicating work. (Perhaps something to share?)



Solution 1:[1]

Call existing Java YAML import libraries from Matlab. This should be pretty strightforward. Java-Matlab interoperability is pretty good, see matlab help.

Solution 2:[2]

There is now a matlab wrapper for snakeyaml:

http://code.google.com/p/yamlmatlab/

I've tried it out and it works pretty well on a simple yaml file. There are bugs if the yaml field names are not valid fieldnames and when strings are empty. I've suggested workarounds for both of these in the issues section on the google code site

Solution 3:[3]

I believe, that the following example can save some time.

First you need to load a YAML document using snake YAML:

javaaddpath C:\temp\snakeyaml-1.7.jar
import('org.yaml.snakeyaml.Yaml')

yamlreader = Yaml();
yml = fileread('C:\temp\test.yml');
jymlobj = yamlreader.load(yml);

and afterwards you will need to access data from the java object, I did it using the following routine (note that this is not a general routine and it was developed for a particular type of YAML files, where for example matrices are defined as in Matlab)

function Data = Hash2Struct(hashMap)

  Data = [];

  iterator = hashMap.keySet().iterator();
  while (iterator.hasNext())
     field = iterator.next();
     if ~isempty(field)
        d =  hashMap.get(field);
        switch class(d)
            case {'java.util.LinkedHashMap'}
                Data.(field) = Hash2Struct(d);
            case {'java.util.ArrayList'}
               Data.(field) = str2num(d.toString);               
            otherwise    
                Data.(field) = d;
        end
     end
  end

Good luck, Jan

Solution 4:[4]

@AndyL For sankeyaml, add it to your javapath, then import('org.yaml.snakeyaml.Yaml'), and it works like the docs; the resulting LinkedHashMap is accessed more or less like a matlab struct.

Solution 5:[5]

There is also MEX implementation by Geoffr Adams called mat-yaml with C binding for libyaml parser-emitter).

But it can not be compiled on Windows yet.

Solution 6:[6]

ZozaniDB Database Toolbox includes a Matlab-native implementation for YAML (distributed as p-files).

>> s=yaml_dump(struct('name','apple','calories',53))

  s =

name: apple
calories: 53

>> yaml_parse(s)

ans = 
        name: 'apple'
    calories: 53

Solution 7:[7]

I wrote a wrapper for SnakeYAML:

https://www.mathworks.com/matlabcentral/fileexchange/106765-yaml

>> data.a = 1.23;
>> data.b = "hello";
>> data.c = {2, {true, 'world'}, yaml.Null};

>> s = yaml.dump(data)

    "a: 1.23
     b: hello
     c:
     - 2.0
     - [true, world]
     - null
     "
   
>> result = yaml.load(s)

  struct with fields:

    a: 1.2300
    b: "hello"
    c: {[2]  {1×2 cell}  [1×1 yaml.Null]}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mikhail Poda
Solution 2 Marc
Solution 3 Kyle Simek
Solution 4 natto
Solution 5 Yauhen Yakimovich
Solution 6 ahmetsacan
Solution 7 MartinKoch