Home Integrating Matlab into Java with MatlabEngine
Reply: 0

Integrating Matlab into Java with MatlabEngine

user1962
1#
user1962 Published in May 24, 2018, 11:22 pm

I am trying to run Matlab scripts with Java to create plots and display them on a GUI built in Java.

When I try to run the code from the main JPanel constructor, it works fine, but when I put it inside an ActionListener, I get this exception :

java.lang.IllegalStateException: Blocking in Event Dispatch Thread not allowed

Using the startMatlabAsync() gives me the error (I though that might do the trick?).

Since my ultimate goal is to have a fully functional GUI from which I can run different scripts, them not working when they are called from an even listener is quite problematic...

Here is the code :

            try {
                final String plotPath = "the soon to be plot";

                // Future<MatlabEngine> asyncEng = MatlabEngine.startMatlabAsync();
                // MatlabEngine eng = asyncEng.get();

                String inputPath = "get me data";
                String outputPath = "give me plot";

                MatlabEngine eng = MatlabEngine.startMatlab();

                eng.eval("cd somewhere elso");
                eng.eval("plotMe(" + inputPath + ", " + outputPath + ")");

                eng.close();

                BufferedImage img = ImageIO.read(new File(plotPath));
                ImageIcon icon = new ImageIcon(img);
                JLabel image = new JLabel(icon);

                MainPanel.this.add(image);
            }
            catch (SomeException ex) {
                ex.printStackTrace();
            }

When I put the code inside a an event listener, like this :

    private class ButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button1) {
            createSomeDataToPlot();

            // same code as above
        }
    }
}

BIG PROBLEMS.

Thanks in advance!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299484 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO