java,matlabcontrol,使用eval和feval

时间:2015-09-15 16:40:25

标签: java eval matlabcontrol

我在matlabcontrol liblary方面遇到问题,尤其是使用eval和feval函数。 这是我的程序

    package matlab;

    import java.io.IOException;
    import java.util.Arrays;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import matlabcontrol.MatlabProxy;
    import matlabcontrol.MatlabProxyFactory;
    import matlabcontrol.MatlabConnectionException;
    import matlabcontrol.MatlabInvocationException;
    import matlabcontrol.extensions.MatlabNumericArray;
    import matlabcontrol.extensions.MatlabTypeConverter;

    public class Matlab {

        public static void main(String[] args)  {

            String address = "D:\\database.mat";
            int hidden = 10;
            int epoch = 100;
            int mu = 1;
            int lr =1;
            int fail =1;
            try {

        MatlabProxyFactory factory = new MatlabProxyFactory();
        MatlabProxy proxy = factory.getProxy();


        proxy.eval("load"+address);
//there is problem here unclosed string literal
        proxy.eval("net=newff(input,target,["+hidden+"),(logsig, tansig),trainlm);
        proxy.eval("net.trainparam.showwindow=true;");
        proxy.eval("net.divideRand='trainlm';");
        proxy.eval("net.trainparam.epochs="+epoch+";");
        proxy.eval("net.trainparam.mu="+mu+";");
        proxy.eval("net.trainparam.lr="+lr+";");
        proxy.eval("net.trainparam.goal=0;");
        proxy.eval("net.trainparam.max_fail="+fail+";");
        proxy.eval("net=train(net,input,target);");
        proxy.eval("class_Matlab=sim(net,target);");
        proxy.eval("save net net class_Matlab");




        proxy.disconnect();
            } catch (MatlabConnectionException ex) {
                Logger.getLogger(Matlab.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MatlabInvocationException ex) {
                Logger.getLogger(Matlab.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

我仍然不知道如何正确使用eval和feval,有人可以解释我如何使用eval和feval吗?

我使用mathworks中的示例测试了库,并且工作正常

package matlab;

import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;
import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.extensions.MatlabNumericArray;
import matlabcontrol.extensions.MatlabTypeConverter;

public class Matlab {

    public static void main(String[] args)  {


        try {

    MatlabProxyFactory factory = new MatlabProxyFactory();
    MatlabProxy proxy = factory.getProxy();


    proxy.eval("[x,t] = simplefit_dataset;");
    proxy.eval("net= feedforwardnet(10);");
    proxy.eval("net = train(net,x,t);");
    proxy.eval("view(net)");



    proxy.disconnect();
        } catch (MatlabConnectionException ex) {
            Logger.getLogger(Matlab.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MatlabInvocationException ex) {
            Logger.getLogger(Matlab.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
{
MatlabProxyFactoryOptions options =
                    new MatlabProxyFactoryOptions.Builder()
                        .setUsePreviouslyControlledSession(true)
                        .build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();
proxy.feval("matlab_filename");
proxy.disconnect();
}

public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException { //Create a proxy, which we will use to control MATLAB MatlabProxyFactory factory = new MatlabProxyFactory(); MatlabProxy proxy = factory.getProxy();
//Set a variable, add to it, retrieve it, and print the result
proxy.setVariable("a", 5);
proxy.eval("a = a + 6");
Object result = proxy.getVariable("a");
System.out.println("Result: " + result);

//Disconnect the proxy from MATLAB
proxy.disconnect();
}

Public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException { //Create a proxy, which we will use to control MATLAB MatlabProxyFactory factory = new MatlabProxyFactory(); MatlabProxy proxy = factory.getProxy();

//Display 'hello world' like before, but this time using feval
proxy.feval("disp", "hello world");

//Disconnect the proxy from MATLAB
proxy.disconnect();
} 

如需更多帮助,请先执行示例。 https://code.google.com/archive/p/matlabcontrol/wikis/Walkthrough.wiki

希望它会对你有所帮助。