0

我使用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();
  }
4

1 回答 1

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
于 2012-07-01T08:36:12.970 回答