在 Java 对象中存储 MATLAB 结构
我在 MATLAB 中使用 Java HashMap
h = java.util.HashMap;
虽然字符串、数组和矩阵可以无缝地使用它,但
h.put(5, 'test');
h.put(7, magic(4));
结构却不能
h=java.util.HashMap;
st.val = 7;
h.put(7, st);
??? No method 'put' with matching signature found for class 'java.util.HashMap'.
使它适用于结构的最简单/最优雅的方法是什么?
I'm using Java HashMap in MATLAB
h = java.util.HashMap;
And while strings, arrays and matrices works seemlessly with it
h.put(5, 'test');
h.put(7, magic(4));
Structs do not
h=java.util.HashMap;
st.val = 7;
h.put(7, st);
??? No method 'put' with matching signature found for class 'java.util.HashMap'.
What would be the easiest/most elegant way to make it work for structs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要确保从 MATLAB 传递到 Java 的数据可以正确转换。请参阅 MATLAB 的外部接口文档,了解要转换的类型的转换矩阵到其他类型。
MATLAB 将大多数数据视为按值传递(具有句柄语义的类除外),并且似乎没有办法将结构包装在 Java 接口中。但是您可以使用另一个 HashMap 来充当结构体,并将 MATLAB 结构体转换为 HashMaps(对多级结构体、函数句柄以及其他不能很好地适应 MATLAB/Java 数据转换过程的野兽有明显的警告) 。
一个可能的用例:(
给读者的一个练习:将其更改为对本身就是结构的结构成员递归地工作)
You need to ensure that the data passed from MATLAB to Java can be properly converted. See MATLAB's External Interfaces document for the conversion matrix of which types get converted to which other types.
MATLAB treats most data as pass-by-value (with the exception of classes with handle semantics), and there doesn't appear to be a way to wrap a structure in a Java interface. But you could use another HashMap to act like a structure, and convert MATLAB structures to HashMaps (with an obvious warning for multiple-level structures, function handles, + other beasts that don't play well with the MATLAB/Java data conversion process).
a possible use case:
(an exercise for the reader: change this to work recursively for structure members which themselves are structures)
Matlab R2008b 和更新版本有一个Containers.Map 类,它在本机Matlab 数据类型上提供类似HashMap 的功能,因此它们可以处理结构、单元格、用户定义的Matlab 对象等。
Matlab R2008b and newer have a containers.Map class that provides HashMap-like functionality on native Matlab datatypes, so they'll work with structs, cells, user-defined Matlab objects, and so on.
我不熟悉 Java HashMap,但是您可以尝试使用元胞数组而不是结构来存储数据吗?
I'm not familiar with Java HashMaps, but could you try using a cell array to store the data instead of a struct?