MATLAB:读取返回的java对象数组

时间:2015-03-11 02:35:07

标签: java matlab matlab-java

我有一个自定义java对象,它返回Thing个java对象的数组。如何在matlab中将返回的数组转换并迭代为java对象?目前,它看起来并不像返回的数组被视为java对象。

//Thing.java
public class Thing {

    public int x,y,z;
    public int[][] board;
    // constructors and setters etc.
}

//ThingReturner.java
public class ThingReturner {
    private LinkedList<Thing> mQueue = new LinkedList<>();

    public void putThing(int x, int y, int z, int[][] board) {
         mQueue.add(new Thing(x, y, z, board));
    }

    public Thing[] getThings() {
        Thing[] things = new Thing[mQueue.size()];
        while (!mQueue.isEmpty()) {
            mQueue.pop();
        }
        return things;
    }
}

在我的MATLAB代码中,我有:

javaaddpath '../bin/';
obj = javaObjectEDT('ThingReturner');
obj.putThing(4,6,0,[[1,1,1],[1,0,0],[1,1,1]]);
obj.putThing(2,2,1,[[1,0,1],[1,1,1],[1,0,1]]);
>> obj.getThings()

ans =

Thing[]:
    []
    []

调用isjava(ans(1))返回0(isjava(ans)返回1)。那么如何正确使用getThings()的结果,以便我可以使用ans(1).getX()等?

1 个答案:

答案 0 :(得分:0)

我在Java代码中犯了一个愚蠢的错误,应该是:

public GameOfLifeThing[] getThings() {
    GameOfLifeThing[] things = new GameOfLifeThing[mGoLQueue.size()];
    int i =0;
    while (!mGoLQueue.isEmpty()) {
        things[i++] = mGoLQueue.pop();
    }
    return things;
}