user1312837
user1312837

Reputation: 1298

Matlab java interop. Use enum values

I have java enum

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

In matlab(2012a) I load appropriate jar, and set

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

It works as well. But when i try to eval

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 disallow me to get 's' and 'p' enum values?

P.S.

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 question remains relevant for me.

Upvotes: 4

Views: 306

Answers (3)

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

As is listed in this answer, you might need to reference it a bit differently. Sometimes Java stores things as $ instead of ., so if you make the call as listed below, it should work.

javaObject('com.moc.MyType$q')

Upvotes: 2

Pursuit
Pursuit

Reputation: 12345

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

To confirm/refute this sort of an error, make the smallest change you can which will be visible when you execute. For instance, change q to qq and confirm that a = com.moc.MyType.qq; works in Matlab.

If it does work, then I'm wrong. If it does not, than you need to go through your compilation/jar'ing/loading process to see where you have a stale file.

Upvotes: 1

mbelow
mbelow

Reputation: 1133

Hm, this should work. I'm just guessing, but could you try with upper case enum constants (e.g. com.moc.MyType.P) Maybe there is some sloppy stuff going on that relies on convention.

Upvotes: 1

Related Questions