在matlabcontrol中使用eval时出错

时间:2012-07-01 06:20:26

标签: java matlab matlab-deployment

我使用MatlabConrol连接Java和MATLAB。我想向MATLAB发送一个图像路径,用匹配的函数处理它,并给我一些类似的图像和路径,以便在Java GUI中显示。

将图像路径传递给MATLAB时,我总是得到同样的错误:

Error using eval
Undefined function 'getinput' for input arguments of type 'char'.

这是我的MATLAB函数:

function matlab = getinput(input)
results = hgr(input);

我的Java代码:

imag = ImageIO.read(fileChoose.getSelectedFile());
ImagePath = fileChoose.getSelectedFile().getAbsolutePath();

public void SendingMatlabControl() throws MatlabConnectionException,
  MatlabInvocationException {
  // create proxy
  MatlabProxy proxy;
  // Create a proxy, which we will use to control MATLAB
  MatlabProxyFactory factory = new MatlabProxyFactory();
  proxy = factory.getProxy();
  // Display 'hello world' like before, but this time using feval
    try {
      // call builtin function
      proxy.eval("getinput('imagepath')");
      // call user-defined function (must be on the path)
      proxy.eval("addpath('E:\\vm')");
      proxy.feval("matlab");
      proxy.eval("rmpath('E:\\vm)");
    } catch (MatlabInvocationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    // Disconnect the proxy from MatLab
    proxy.disconnect();
  }

1 个答案:

答案 0 :(得分:3)

让我举一个调用函数,传递输入和检索输出的例子。有两种方法,可以:

  • 使用eval并在MATLAB工作区内分配函数的结果,然后使用getVariable
  • 检索这些变量
  • 使用returningFeval来评估函数及其输入,并直接检索输出。

假设我们有一个MATLAB函数 myfunc.m ,它接受一个字符串作为输入,并返回一个包含字符串,数字和向量的单元格数组:

function out = myfunc(str)
    out = cell(3,1);
    out{1} = sprintf('Welcome to %s!', str);
    out{2} = 99;
    out{3} = rand(10,1);
end

这是Java代码:

import matlabcontrol.*;

public class TestMyFunc
{
    public static void main(String[] args)
        throws MatlabConnectionException, MatlabInvocationException
    {
        // create proxy
         MatlabProxyFactoryOptions options =
            new MatlabProxyFactoryOptions.Builder()
                .setUsePreviouslyControlledSession(true).build();
        MatlabProxyFactory factory = new MatlabProxyFactory(options);
        MatlabProxy proxy = factory.getProxy();

        // call function and get output cell array
        String in = "Stack Overflow";
        Object[] out = proxy.returningFeval("myfunc", 1, in);

        // extract stuff from cell array
        out = (Object[]) out[0];
        String str = (String) out[0];
        double x = ((double[]) out[1])[0];
        double[] arr = (double[]) out[2];

        // show result
        System.out.println("str =\n " + str);
        System.out.println("x = \n " + x);
        System.out.println("arr =");
        for(int i=0; i < arr.length; i++) {
            System.out.println(" " + arr[i]);
        }

        // shutdown MATLAB
        //proxy.feval("quit");

        // close connection
        proxy.disconnect();
    }
}

我得到了输出:

str =
 Welcome to Stack Overflow!
x =
 99.0
arr =
 0.5974901918725793
 0.3353113307052461
 0.29922502333310663
 0.45259254156932405
 0.42264565322046244
 0.35960631797223563
 0.5583191998692971
 0.7425453657019391
 0.42433478362569066
 0.42935578857620504