001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.plugins.connector.matlab;
006    
007    import jmatlink.JMatLink;
008    
009    import javax.swing.*;
010    import java.io.File;
011    import java.io.FileNotFoundException;
012    import java.lang.reflect.InvocationTargetException;
013    import java.text.MessageFormat;
014    
015    /**
016     * @author Azin Azadi , Mohammad Ali Rostami
017     * @email ma.rostami@yahoo.com
018     */
019    public class MatlabRunner {
020    
021        public static jmatlink.JMatLink engine;
022    
023        /**
024         * runs the given matlab (.m) file as a function with the given params as the
025         * inputs of function in the Matlab environment.
026         *
027         * @param matlabfile
028         * @param params
029         * @return the result of function
030         * @throws FileNotFoundException
031         * @throws IllegalAccessException
032         * @throws InvocationTargetException
033         */
034        public static String matlabRunner(File matlabfile, Object[] params) throws FileNotFoundException, IllegalAccessException, InvocationTargetException {
035            try {
036                engine = new JMatLink();
037                engine.engOpen();
038                engine.setDebug(false);
039                engine.engOutputBuffer();
040            } catch (Throwable e) {
041                JOptionPane.showMessageDialog(null, "Matlab Engine could not be oppened!", "Error while starting MatLab", JOptionPane.ERROR_MESSAGE);
042                return "Matlab Engine could not be oppened!";
043            }
044            String command = "";
045            String path = matlabfile.getParentFile().getPath();
046            path = path.replaceAll("%20", " ");
047            engine.engEvalString("cd ('" + path + "')");
048            command = "varvar = " + matlabfile.getName().substring(0, matlabfile.getName().length() - 2) + "(";
049            for (Object p : params)
050                command += p + ",";
051            if (params.length > 0)
052                command = command.substring(0, command.length() - 1);
053            command += ")";
054    //        command = "aa = h";
055            //   engine.engO
056            System.out.println(MessageFormat.format("command:{0}", command));
057            engine.engEvalString(command);
058    //        engine.eng
059    //        return engine.engGetCharArray("aa")[0];
060    
061            String ret = engine.engGetOutputBuffer();
062            if (ret.startsWith("???")) {
063                JOptionPane.showMessageDialog(null, ret, "Error while running MatLab", JOptionPane.ERROR_MESSAGE);
064                return ret;
065            }
066            ret = ret.replaceAll("\n", " ");
067            ret = ret.substring(ret.indexOf("=") + 1).trim();
068            return ret;
069        }
070    }