Matlab java interop. Use enumeration values

I have java enum

package com.moc; public enum MyType { s, q, p, none, } 

In matlab (2012a), load the appropriate jar and install

 a = com.moc.MyType.q; a = com.moc.MyType.none; 

It works. But when I try to appreciate

 a = com.moc.MyType.s; 

or

 a = com.moc.MyType.p; 

I get an exception

 The class com.moc.MyType has no property or method named 's'. 

and

 The class com.moc.MyType has no property or method named 'p'. 

Why did Matlab forbid me to get the values โ€‹โ€‹of 's' and 'p' enum?

PS

 a = com.moc.MyType.q; a.getClass.getDeclaredField('p') 

returns

 public static final com.moc.MyType com.moc.MyType.p 

UPDATE

com.moc.MyType.valueOf ('p') works. But the question remains relevant to me.

+4
source share
3 answers

As indicated in this answer , you may need to reference it a little differently. Sometimes Java stores things like $ instead . therefore, if you make a call as follows, it should work.

 javaObject('com.moc.MyType$q') 
+2
source

Hm, that should work. I just guess, but you could try with uppercase overflow constants (for example, com.moc.MyType.P) There might be some messy behavior that depends on the agreement.

+1
source

I would really suspect this is a configuration error. (What I used to call the makefile). That is, I suspect that the code you are actually loading into Matlab is not related to the Java code you are writing, but is an older version.

To confirm or deny this type of error, make the smallest change that you can see when you execute it. For example, change q to qq and make sure a = com.moc.MyType.qq; works in matlab.

If this works, Iโ€™m wrong. If this is not the case, you need to go through the compilation process / jar 'ing / loading to see where your legacy file is.

+1
source

All Articles