views:

160

answers:

1

I have a java.util.HashMap object that I want to convert generally into a MatLab datatype, perhaps the new containers.Map type.

Ideally I could do:

it = javaHashMapObj.keySet.iterator;
while it.hasNext
    jkey = it.next;
    someMatlabObj(jkey) = javaHashMapObj.get(jkey);
end

Among other potential problems (please point out, resolve if they jump out at you!), there is the issue that if the Java HashMap is keyed with integers, it.next will nevertheless return MatLab double objects, which will then not work as keys into the HashMap with javaHashMapObj.get.

Can someone suggest a way to resolve this? Extend the Java object to give me the MatLab int32 for the keys?

+2  A: 

MATLAB will convert a regularly entered number (which is a double) to a Java primitive int, but if you want an Integer Object, you have to explicitly box it yourself:

javaHashMapobj.get(java.lang.Integer(key));

See http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f6425.html for a table of how data types are converted from MATLAB to Java.

Mike Katz
Thanks Mike, this is sort of the inverse of what I actually wanted to know, but still helpful. My problem is that when it.next should return a key that in Java is an Int, MatLab gets the key as a double. Then when I try to use it.next to get the values from the Java object they don't work because the Java hash only has Int keys.Of course, I could cast it.next to a MatLab int using int32(it.next) and then that value would work as a key into the Java hash. But I am trying to have a way to handle this without a priori knowledge of the keys.
Chinasaur