2

I have an custom java object that returns an array of Thing java objects. How can I convert and iterate over the returned array as java objects in matlab? Currently, it doesn't look like the returned array is being treated as java objects.

//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;
    }
}

In my MATLAB code, I have:

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[]:
    []
    []

Calling isjava(ans(1)) returns 0 (isjava(ans) returns 1 though). So how do I properly use the result of getThings() so I can use ans(1).getX() etc.?

Jonno_FTW
  • 8,601
  • 7
  • 58
  • 90

1 Answers1

0

I made a silly mistake in my Java code, it should be:

public GameOfLifeThing[] getThings() {
    GameOfLifeThing[] things = new GameOfLifeThing[mGoLQueue.size()];
    int i =0;
    while (!mGoLQueue.isEmpty()) {
        things[i++] = mGoLQueue.pop();
    }
    return things;
}
Jonno_FTW
  • 8,601
  • 7
  • 58
  • 90