MATLAB 是否已有 YAML 库/解析器?

发布于 2024-08-08 18:50:39 字数 240 浏览 3 评论 0 原文

我想使用 YAML 跨多种语言交流一些数据。 (将其视为“与语言无关的序列化”。)其中一种语言是 MATLAB,但我似乎找不到该语言的 YAML 库。我在 Google 上检查了“matlab yaml”和“matlab yaml parse”,似乎没有任何相关结果。在 MATLAB Central 上搜索“yaml”也让我一无所获。

MATLAB 真的没有现有的 YAML 库吗?我很乐意写一份,但我想避免重复工作。 (也许有什么可以分享的?)

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?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

蘸点软妹酱 2024-08-15 18:50:39

我扩展了 Jan 的代码以支持更多数据类型以及将 matlab 结构写入 yaml 文件。它不仅仅是一个 m 文件,因此我在 http://code.google.com 上找到了源代码/p/yamlmatlab/
吉里

I extended Jan's code to support more data types as well as writing out of a matlab struct to yaml file. It is not just one m-file therefore I located the sources on http://code.google.com/p/yamlmatlab/
Jiri

爱给你人给你 2024-08-15 18:50:39

从 Matlab 调用现有的 Java YAML 导入库。这应该是非常简单的。 Java-Matlab 互操作性非常好,请参阅 matlab 帮助。

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

瑕疵 2024-08-15 18:50:39

现在有一个 Snakeyaml 的 Matlab 包装器:

http://code.google.com/p/yamlmatlab/< /a>

我已经尝试过了,它在简单的 yaml 文件上运行得很好。如果 yaml 字段名称不是有效的字段名称以及字符串为空,则会出现错误。我在谷歌代码网站的问题部分建议了这两个问题的解决方法

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

假装爱人 2024-08-15 18:50:39

我相信,下面的例子可以节省一些时间。

首先,您需要使用 Snake YAML 加载 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);

然后您需要从 java 对象访问数据,我使用以下例程完成它(请注意,这不是通用例程,它是为特定类型的YAML 文件,例如矩阵的定义与 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

祝你好运,

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

∞梦里开花 2024-08-15 18:50:39

@安迪L
对于 sankeyaml,将其添加到您的 javapath,然后 import('org.yaml.snakeyaml.Yaml'),它的工作方式与文档类似;生成的 LinkedHashMap 的访问或多或少类似于 matlab 结构体。

@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.

何以心动 2024-08-15 18:50:39

还有 Geoffr Adams 的 MEX 实现,名为 mat-yaml,带有 C 绑定 libyaml 解析器发射器)。

但目前还不能在Windows 上编译。

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.

尝蛊 2024-08-15 18:50:39

ZozaniDB Database Toolbox 包含 YAML 的 Matlab 原生实现(以 p 文件形式分发)。

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

  s =

name: apple
calories: 53

>> yaml_parse(s)

ans = 
        name: 'apple'
    calories: 53

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
零度℉ 2024-08-15 18:50:39

我为 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]}

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]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文