有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将变量值传递给eval函数

当执行这段代码时,程序使用matlabcontrol库打开MATLAB并在eval()中执行给定的行。问题出现在第一行,即m.eval("image1=imread('D:/My Hand Gestures/f.jpg');");将一个固定的字符串值作为输入。但在这里,我想将路径存储在一个变量中,并将其传递给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) 个答案

  1. # 1 楼答案

    我想先试试这个。看看是否有帮助

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