3

I have a requirement to produce graphs of matrices and display these graphs on a JSP. The project has been developed in Java and so far all my operations relating to matrices are being performed using the MatLabControl API http://code.google.com/p/matlabcontrol/ .

I wanted to return the matrices produced by MATLAB (especially eigen value matrices and wavelets). MATLAB provides a function "im2java" that converts graph image from its MATLAB representation to a java.awt.Image. My code used to get the image data in MatlabControl was as follows:

public Image produceEigenValueGraph(final double [][] matrix) {
final double [][] maxEigenValueMatrix =
            extractOutMaxEigenValues(matrix);
    Image matlabPlotImage = null;
    try {
        MatlabNumericArray matLabEigenValueMatrix =
                new MatlabNumericArray(maxEigenValueMatrix, null);
        matLabTypeConverter.setNumericArray("eigen", 
                                            matLabEigenValueMatrix);
        matLabProxy.setVariable("amountOfTime", matrix.length - 1);
        matLabProxy.eval("time");
        matLabProxy.eval("plot(time, eigen)");
        matLabProxy.eval("frame=getframe");
        final Object [] returnedMatlabArguements =
                matLabProxy.returningEval("im2java(frame.cdata)", 1);
        matlabPlotImage =
                (Image)returnedMatlabArguements[0];
    } catch (MatlabInvocationException mie) {
        mie.printStackTrace();
    }
    return matlabPlotImage;
}

The code returns a nested exception:

Caused by: java.io.WriteAbortedException: writing aborted; 
java.io.NotSerializableException: sun.awt.image.ToolkitImage

Which basically puts an end to any hope of the above code working, unless I am incorrect in my use.

N.B The code does produce a correct graph it fails to return it in java.awt.Image

My questions are:

    -Is the above code the correct/only way to return images to a java program from Matlab?
    -If it is what would be the best alternatives to using Matlab, Java API or otherwise?
zellus
  • 9,617
  • 5
  • 39
  • 56
OpelMac
  • 95
  • 1
  • 1
  • 9

1 Answers1

0

Is this the line that causes the exception?

matlabPlotImage = (Image)returnedMatlabArguements[0];

In answer to your question

"-Is the above code the correct/only way to return images to a java program from Matlab?"

You can call java classes from Matlab so you could also use the java in a Matlab file and call that to replace

final Object [] returnedMatlabArguements = matLabProxy.returningEval("im2java(frame.cdata)", 1);
matlabPlotImage = (Image)returnedMatlabArguements[0];

The error is being thrown because Image is not serializeable. An option would be to save it as a file in some image format (jpg,png,tiff) using either matlab or java and return File instead of Image.

"-If it is what would be the best alternatives to using Matlab, Java API or otherwise?"

Mathworks provide a Java api to perform a number of linear algebra calculations that you could implement. http://math.nist.gov/javanumerics/jama/#Package

Alternatively the Apache Commons Math project provide a wide range of linear algebraic functions as well as other tools. http://commons.apache.org/math/userguide/linear.html

I would check other posts for suggestions on graphing in java

constructing graphs in Java

Java Graphing Libraries for Web Applicattions?

Community
  • 1
  • 1
Codey McCodeface
  • 2,988
  • 6
  • 30
  • 55