Search code examples
javaarraysmatlabobjectjavabuilders

Converting a MATLAB Javabuilder output object element to an array


I've been issued the task of using MATLAB Builder to convert a MATLAB function into a Java class, now I have gotten to a point where I have the results from one class being fed into another and since the MATLAB builder will only output an object, I'm having issues.

import java.util.*;

import com.mathworks.toolbox.javabuilder.*;

public class mainKrigTau {

public static void main(String[] args) {

    Object[] resultT = null;
    Object[] resultK = null;
    Object[] resultB = null;
    krigingTau Tau = null;
    krigingTau Krig = null;
    Branin branin = null;

    try {

        Tau = new krigingTau();
        Krig = new krigingTau();
        branin = new Branin();
        resultT = Tau.LPtau(1, 100, 2, 1234);
        List<Object> X = Arrays.asList(resultT[0]);
        System.out.println(X);
        System.out.println((X.size()));


    } catch (MWException e) 
            {
        e.printStackTrace();
    } finally 
            {
    }

So basically the output of the Tau class is a 2D array, so the array is embedded into the resultT[] object, how to I get access to this array? One method I have tried is as above, changing it into an array list, X outputs the array, but I can't access the components of the array. X.size = 1, not 100 which is the actual size of the array. edit - just to add, all the examples I can find just output the result object and display it, rather than do anything with it.


Solution

  • Okay a software engineer nearby came to the rescue. It's a bit of a roundabout way but it works.

        try {
    
            Tau = new krigingTau();
            Krig = new krigingTau();
            branin = new Branin();
    
            resultT = Tau.LPtau(1, 100, 2, 1234);
            List<Object> X = Arrays.asList(resultT[0]);         
    
            if (X.get(0) instanceof MWNumericArray) {
    
                MWNumericArray mw= (MWNumericArray) X.get(0);
                ArrayList<Point> lists = new ArrayList<Point>();
                for(int i = 1; i <= mw.numberOfElements()/2; i++){
                    Object o = mw.get(i);
                    System.out.println(o);
                    Double x = (Double) mw.get(i);
                    Double y = (Double) mw.get((mw.numberOfElements()/2)+i);
                    Point p = new Point(x.doubleValue(),y.doubleValue());
                    lists.add(p);
                }
    
                for(int i = 1; i < mw.numberOfElements()/2; i++)
                {
    
                    resultB = branin.branin(1, lists.get(i).array);
                    System.out.println(resultB[0]);
                }
    

    where point sets the values of x and y into an array, so set it to an new MWNumericArray and do Object.get(i) but this flattens the array which is not ideal.