Giter VIP home page Giter VIP logo

yaml's Introduction

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

yaml's People

Contributors

martinkoch123 avatar alec-hoyland avatar

Stargazers

Procat avatar avikus-doyeop.lee avatar PeterC avatar  avatar Yixiang Niu avatar Yoshitomo KANEDA avatar Guillaume David avatar  avatar Postrantor avatar Dylan Winters avatar Kevin Lorenz avatar Cameron Brown avatar  avatar Jianeng Wang avatar Michal Kvasnicka avatar David Wilby avatar Maarten Afschrift avatar Alex_Cui avatar 崔星星 avatar Roberto Agromayor avatar Lionel RIGOUX avatar Bill Tubbs avatar

Watchers

Michal Kvasnicka avatar  avatar

yaml's Issues

Best way to create a cell array of character vectors instead of strings

Excellent, and useful tool. Thanks.

I'm using it to store information on variables with names. For example, my Yaml file might contain:

cvs:
  names: [SAG_WT, SAG_POW, SAG_CC, SAG_OF_P80]

After loading the yaml file to a struct called model_data these names become a cell array as expected:

>> model_data.cvs.names

ans =

  1×4 cell array

    {["SAG_WT"]}    {["SAG_POW"]}    {["SAG_CC"]}    {["SAG_OF_P80"]}

However, tables don't seem to accept cell arrays of strings and seem to require char

>> array2table(rmse, 'VariableNames', model_data.cvs.names)
Error using table (line 326)
The VariableNames property is a cell array of character vectors. To assign multiple variable names, specify names in a string
array or a cell array of character vectors.

Is there a way to force a cell array of character vectors instead of strings? Or, any other ideas on how to easily populate the RowNames or VariableNames properties of tables?

Unusual line continuation behaviour with yaml.Null

This is fairly minor, but the line break and continuation seems a little premature for an array of Nulls:

>> yaml.dump({yaml.Null, yaml.Null, yaml.Null, yaml.Null, yaml.Null})

ans = 

    "[null, null, null, null,
       null]
     "

performence bechmarks

Are there any performance benchmarks? How compare this parser with other MATLAB parsers available regarding large YAML files?

Minor typo in the docstring for dumpFile

I think there is a typo in the help text of yaml.dumpFile:

>> help yaml.dumpFile
  dumpFile Write data to YAML file.
    yaml.dumpFile(FILE, DATA) converts DATA to YAML and saves it in a new
    file FILE.
 
    yaml.dumpFile(DATA, STYLE) uses a specific output style.
    STYLE can be "auto" (default), "block" or "flow".

I think the second case should be yaml.dumpFile(FILE, DATA, STYLE)

Cannot find 'Yaml' function

I'm getting this error

Unrecognized function or variable 'Yaml'.
 
44      MException("yaml:load:Failed", "Failed to load YAML string.").addCause(cause).throw

any ideas where this function is supposed to be located?

YAML 1.2

Do you plan to upgrade your yaml parser for MATLAB to be compatible with YAML 1.2 specs via latest snakeyaml-engine?

Problem with 'dumpFile'

Hi, Thank you for designing this tool.

I'm trying to write a simple struct to a yaml file using 'dumpFile' function. Here is what I write

data_towrite = strcut('int_data', int32(1), 'str_data', 'str');
yaml.dumpFile("my.yaml",data_towrite);

But I get the error messages for yaml.dumpFile("my.yaml",data_towrite);:

Check for incorrect argument data type or missing argument in call to function 'setDefaultFlowStyle'.

Error in yaml.dump/getDumperOptions (line 145)
        opts.setDefaultFlowStyle(styleFields(styleIndex).get([])); %

Error in yaml.dump (line 56)
dumperOptions = getDumperOptions(style);

Error in yaml.dumpFile (line 42)
yamlString = yaml.dump(data, style);

Could you please help me with that?

How do I parse this .yml format file?

hi, I need to parse such a .yml file into matlab, or later I need to save the variables in matlab in similar .yml format, but I can't import and export successfully, I expect the author to enhance this function!

s = yaml.loadFile("cam_proj_CAM_FRONT.yml");

The output error message is as follows:

Error using yaml.load
Failed to load YAML string.
Error in yaml.loadFile (line 44)
result = yaml.load(content, "ConvertToArray", options.ConvertToArray);
Caused by:
    Error using yaml.load
...

cam_proj_CAM_FRONT.zip

Link to question information

Out of Memory Error

Hi, I'm trying to load a 78mb yaml file using 'yaml.loadFile' function. But I then get an error message indicating an Out of Memory error. The Current Folder part also becomes blank even I change to a different directory after this error occurs.

Would you please tell me if this problem is solvable? Is that possible to allocate more memory for loading the yaml file?

The full error message is listed below

Error using yaml.load (line 44)
Failed to load YAML string.

Error in yaml.loadFile (line 44)
result = yaml.load(content, "ConvertToArray", options.ConvertToArray);

Error in Tr_beforeRANSAC_Investigation (line 144)
tr_yaml = yaml.loadFile(yaml_tr_name);

Caused by:
    Error using yaml.load (line 42)
    Java exception occurred:
    java.lang.OutOfMemoryError: GC overhead limit exceeded
    	at java.util.HashMap.newNode(HashMap.java:1750)
    	at java.util.HashMap.putVal(HashMap.java:631)
    	at java.util.HashMap.put(HashMap.java:612)
    	at java.util.HashSet.add(HashSet.java:220)
    	at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:132)
    	at org.yaml.snakeyaml.composer.Composer.composeSequenceNode(Composer.java:204)
    	at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:158)
    	at org.yaml.snakeyaml.composer.Composer.composeSequenceNode(Composer.java:204)
    	at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:158)
    	at org.yaml.snakeyaml.composer.Composer.composeSequenceNode(Composer.java:204)
    	at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:158)
    	at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:237)
    	at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:160)
    	at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:123)
    	at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:106)
    	at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:121)
    	at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480)
    	at org.yaml.snakeyaml.Yaml.load(Yaml.java:399)
 
Exception in thread "Explorer NavigationContext request queue": java.lang.OutOfMemoryError: GC overhead limit exceeded
	at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:68)
	at java.lang.StringBuilder.<init>(StringBuilder.java:101)
	at com.mathworks.mlwidgets.explorer.model.vfs.LocationMap.getEntry(LocationMap.java:85)
	at com.mathworks.mlwidgets.explorer.model.vfs.LocationMap.getNearestEntry(LocationMap.java:72)
	at com.mathworks.mlwidgets.explorer.model.vfs.VirtualFileSystem.resolveLocation(VirtualFileSystem.java:289)
	at com.mathworks.mlwidgets.explorer.model.vfs.VirtualFileSystem.getTarget(VirtualFileSystem.java:277)
	at com.mathworks.mlwidgets.explorer.model.vfs.VirtualFileSystem.resolveLocation(VirtualFileSystem.java:286)
	at com.mathworks.mlwidgets.explorer.model.vfs.VirtualFileSystem.getTarget(VirtualFileSystem.java:277)
	at com.mathworks.mlwidgets.explorer.model.vfs.VirtualFileSystem.isMountPoint(VirtualFileSystem.java:240)
	at com.mathworks.mlwidgets.explorer.model.vfs.VirtualFileSystem.toExternalEntry(VirtualFileSystem.java:325)
	at com.mathworks.mlwidgets.explorer.model.vfs.VirtualFileList.getLocationEntry(VirtualFileList.java:47)
	at com.mathworks.mlwidgets.explorer.model.table.UiFileList$PollingNotificationMacPathsWorkaround.<init>(UiFileList.java:545)
	at com.mathworks.mlwidgets.explorer.model.table.UiFileList.diffAndSendEvents(UiFileList.java:418)
	at com.mathworks.mlwidgets.explorer.model.table.UiFileList.updateCache(UiFileList.java:396)
	at com.mathworks.mlwidgets.explorer.model.table.UiFileList.readAndUpdateCache(UiFileList.java:372)
	at com.mathworks.mlwidgets.explorer.model.table.UiFileList.access$500(UiFileList.java:45)
	at com.mathworks.mlwidgets.explorer.model.table.UiFileList$6.run(UiFileList.java:325)
	at com.mathworks.util.RequestQueue.execute(RequestQueue.java:129)
	at com.mathworks.util.RequestQueue.access$000(RequestQueue.java:25)
	at com.mathworks.util.RequestQueue$2.run(RequestQueue.java:79)
	at java.lang.Thread.run(Thread.java:748)

The below part is from my script

Error in Tr_beforeRANSAC_Investigation (line 144)
tr_yaml = yaml.loadFile(yaml_tr_name);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.