2012-07-01 6 views
0

MatlabConrol을 사용하여 Java와 MATLAB을 연결했습니다. MATLAB에 이미지 경로를 전송하여 일치하는 함수로 처리하고 Java GUI에 표시 할 유사한 이미지와 경로를 다시 알려줍니다. MATLAB에 이미지 경로를 통과 할 때matlabcontrol에서 eval을 사용하는 중에 오류가 발생했습니다.

나는 항상 같은 오류가 발생합니다 :

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

그리고 내 자바 코드 :

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(); 
    } 
+0

귀하의 의견 'call builtin function'은'getinput'이 내장 함수임을 시사하는 것으로 보이지만 매쓰 웍스 웹 사이트에서 이에 대한 언급을 찾을 수 없습니다. 이것이 내장 기능이라고 확신합니까? –

답변

3

하자

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

가 여기 내 MATLAB 기능입니다 함수 호출, 입력 전달 및 출력 검색 예제를 보여줍니다. 이 두 가지 방법은, 다음 중 하나

  • 사용 eval과 MATLAB 작업 공간 내부의 함수의 결과를 할당, 다음의 입력을 가진 기능을 평가하기 위해 getVariable, 또는
  • 사용 returningFeval를 사용하는 변수를 검색하고 직접 출력을 검색하십시오.

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

    자바 코드 :

은 가정하자 우리는 MATLAB 함수 문자열을 입력하고, 문자열, 숫자, 및 벡터를 포함하는 셀의 배열을 반환 myfunc.m했다 :

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 
:
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(); 
    } 
} 

나는 출력을 얻을
+0

괜찮 았어. 근데 이제는 모든 fe 파일을 사용할 때 feval을 사용하여 이미지 경로를 전달해야한다. 위의 m을 반환하면 이미지의 출력 번호를 검색 할 수있다. 문제는 여전히 동일한 예외가 있습니다, 위의 코드에서 문제가 생겼습니다. ( – user1318251

+0

@ user1318251 : 아니요, 'returningFeval'을 사용하여 m- 함수를 호출하여 이미지 경로를 전달하고 같은 결과를 얻습니다 이 프로젝트 [Wiki] (http://code.google.com/p/matlabcontrol/wiki/Walkthrough)와 자바 문서 – Amro

+0

을 읽으면서 코드를 실행했지만 "정의되지 않은 함수 'myfunc'가 입력 인 경우 MATLAB 함수를 실행할 때 "이 오류는 myfuncs를 사용하는 중 오류가 발생했습니다 (행 3) 입력 인수가 충분하지 않습니다."문제의 원인을 알고 있습니까? – user1318251

관련 문제