将变量值传递给eval函数

时间:2014-03-19 13:48:37

标签: java matlab

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

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);

        }



}

}

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

1 个答案:

答案 0 :(得分:0)

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

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