2014-03-19 48 views
0

执行这段代码时,程序使用matlabcontrol库打开MATLAB并在eval()中执行给定的行。问题出现在第一行,即m.eval("image1=imread('D:/My Hand Gestures/f.jpg');");将固定的字符串值作为输入。但是,在这里我想将路径存储在一个变量中,并将它传递给imread()函数。我应该怎么做?任何帮助表示赞赏。这是代码。将变量值传递给eval函数

package Interface; 

import matlabcontrol.MatlabConnectionException; 
import matlabcontrol.MatlabInvocationException; 
import matlabcontrol.MatlabProxy; 
import matlabcontrol.MatlabProxyFactory; 
/** 
* 
* @author Rajdeep 
*/ 
public class Output { 

    private static String check; 
    private static String path; 

    Output(){ 
     //default constructor 
    } 

    Output(String s,String p){ 
     check = s; 
     path=p; 
    } 

    //GrayScale Conversion Function 
    public static void grayScale(String p,MatlabProxy m)throws MatlabConnectionException, MatlabInvocationException{ 

    m.eval("image1=imread('D:/My Hand Gestures/f.jpg');"); 
    m.feval("image1","imread(path)"); 
    m.eval("figure,imshow(image1);"); 
    m.eval("image_gray=rgb2gray(image1);"); 
    m.eval("figure,imshow(image_gray);"); 
    m.eval("final_image=imresize(image_gray,0.03125);"); 
    m.eval("figure,imshow(final_image);"); 
    m.eval("imwrite(final_image,'C:/Users/Desktop/f.jpg');"); 


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


    //Median Filtering Function 
    public static void Filter(String p, MatlabProxy m)throws MatlabConnectionException, MatlabInvocationException{ 
    m.eval("I=imread('D:/gestures/f.jpg');"); 
    m.eval("J = medfilt2(I, [4 4]);"); 
    m.eval("figure,imshow(J);"); 
    m.eval("imwrite(J,'C:/Users/Rajdeep/Desktop/f.jpg');"); 


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

    /** 
    * 
    * @param args 
    * @throws MatlabConnectionException 
    * @throws MatlabInvocationException 
    */ 
    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(); 
    Output out=new Output("GrayScale","D:/My Hand Gestures/f.jpg"); 

     if(check == "GrayScale") { 
       grayScale(path, proxy); 
     } 
     if(check== "Filter"){ 
       Filter(path,proxy); 

     } 



} 

} 

这里我创建了一个具有预定义路径的路径变量。我想使用这个变量,而不是像上面提到的过程那样给出路径。

回答

0

我会尝试这个开始。看看它是否有帮助。

String path_variable = "D:/My Hand Gestures/f.jpg";
m.eval("image1=imread('" + path_variable + "');");

+0

谢谢,这确实管用。你救了我很多麻烦。 – Rajdeep

+0

就这么简单。我从来没有这样想过。 – Rajdeep