在Java对象中存储MATLAB结构 [英] Storing MATLAB structs in Java objects

查看:191
本文介绍了在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);

???没有方法'放'与'java.util.HashMap'类匹配的签名。







什么将是最简单/最优雅的方式来使它为结构工作?

解决方案

您需要确保从MATLAB传递给Java的数据可以正确转换。请参阅MATLAB的外部接口文档了解哪些类型转换的转换矩阵其他类型。

MATLAB将大多数数据视为按值传递(除了具有句柄语义的类),并且看起来并不是在Java界面中包装结构的方式。但是您可以使用另一个HashMap来表现结构,并将MATLAB结构转换为HashMaps(对于多层结构,函数句柄,以及其他与MATLAB / Java数据转换过程无关的其他野兽) 。

pre $ function hmap = struct2hashmap(S)
if((〜isstruct(S))||(numel S)〜= 1))
错误('struct2hashmap:invalid','%s',...
'struct2hashmap只接受单个结构');
end

hmap = java.util.HashMap;
for fn = fieldnames(S)'
%fn遍历S
的字段名fn是1x1单元阵列
fn = fn {1};
hmap.put(fn,getfield(S,fn));
end

可能的用例:

 >> M = java.util.HashMap; 
>> M.put(1, A);
>> M.put(2,33);
>> s = struct('a',37,'b',4,'c','bingo')

s =

a:37
b:4
c:'bingo'

>> M.put(3,struct2hashmap(S));
>> M

M =

{3.0 = {a = 37.0,c =宾果,b = 4.0},1.0 = a,2.0 = 33.0}

>>

(对读者的一个练习:将其改为递归地为结构成员本身是结构体)


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?

解决方案

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

function hmap = struct2hashmap(S)
if ((~isstruct(S)) || (numel(S) ~= 1))
    error('struct2hashmap:invalid','%s',...
          'struct2hashmap only accepts single structures');
end

hmap = java.util.HashMap;
for fn = fieldnames(S)'
    % fn iterates through the field names of S
    % fn is a 1x1 cell array
    fn = fn{1};
    hmap.put(fn,getfield(S,fn));
end

a possible use case:

>> M = java.util.HashMap;
>> M.put(1,'a');
>> M.put(2,33);
>> s = struct('a',37,'b',4,'c','bingo')

s = 

    a: 37
    b: 4
    c: 'bingo'

>> M.put(3,struct2hashmap(s));
>> M

M =

{3.0={a=37.0, c=bingo, b=4.0}, 1.0=a, 2.0=33.0}

>>

(an exercise for the reader: change this to work recursively for structure members which themselves are structures)

这篇关于在Java对象中存储MATLAB结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆