简体   繁体   English

在没有Matlab环境的情况下在Java类中运行matlab函数

[英]Run matlab function in java class in absence of matlab environment

I want to use matlab function in java application. 我想在Java应用程序中使用matlab函数。 I create java package from my function by deploytool in matlab. 我通过matlab中的deploytool从函数创建Java包。 Now, how can i use this package? 现在,我该如何使用该软件包? Can only import the jar file created by deploytool in my java project and use its function? 只能在我的java项目中导入由deploytool创建的jar文件并使用其功能吗?

After a lot of googling, I used this toturial but in the final step, i get error "could not load file". 经过大量谷歌搜索后,我使用了此方法,但在最后一步中,出现错误“无法加载文件”。

Also i read about MatlabControl , but in this solution, we should have matlab environment in our system to java code running. 我也阅读了有关MatlabControl的文章 ,但是在此解决方案中,我们应该在系统中使用matlab环境来运行Java代码。 But i will run my final app in systems that may not have matlab at all. 但是我将在可能完全没有matlab的系统中运行我的最终应用程序。

So i need a solution to run matlab function in java class even in absence of matlab environment. 因此,即使在没有matlab环境的情况下,我也需要一种在java类中运行matlab函数的解决方案。

Finally I solve my problem. 最后,我解决了我的问题。 the solution step by step is as follows: 解决方案如下:

  1. write matlab function: 编写matlab函数:

    function y = makesqr(x) 函数y = makeqr(x)

    y = magic(x); y =魔法(x);

  2. Use deploytool in matlab and create java package. 在matlab中使用deploytool并创建Java包。

3.create new java application in Eclipse and add main class. 3.在Eclipse中创建新的Java应用程序并添加主类。 import javabuilde.jar and makesqr.jar: 导入javabuilde.jar和makeqr.jar:

  import com.mathworks.toolbox.javabuilder.MWArray;

  import com.mathworks.toolbox.javabuilder.MWClassID;

  import com.mathworks.toolbox.javabuilder.MWNumericArray;

  import makesqr.Class1;

and main.java: 和main.java:

public class main {

public static void main(String[] args) {

      MWNumericArray n = null;
      Object[] result = null;
      Class1 theMagic = null;

      try
      {
         n = new MWNumericArray(Double.valueOf(5),MWClassID.DOUBLE);

         theMagic = new Class1();

         result = theMagic.makesqr(1, n);
         System.out.println(result[0]);
      }
      catch (Exception e)
      {
         System.out.println("Exception: " + e.toString());
      }
      finally
      {
         MWArray.disposeArray(n);
         MWArray.disposeArray(result);
         theMagic.dispose();
      }
}

} }

  1. add javabuilder.jar and makesqr.jar to java build path of your project. 将javabuilder.jar和makeqr.jar添加到项目的Java构建路径

  2. run it. 运行。

the Double.valueOf(3), define the input for our function and the output is as follows: Double.valueOf(3),定义函数的输入,输出如下:

 8     1     6
 3     5     7
 4     9     2

I didn't get properly your problem. 我没有正确解决您的问题。 Did you already compile the jar file from Matlab code and you are trying to use that, or you are at the last step of the tutorial? 您是否已经从Matlab代码中编译了jar文件并且正在尝试使用该文件,或者您正处于本教程的最后一步?

If your answer is the latest case, most probably you forgot the "." 如果您的答案是最新情况,则很可能您忘记了“。” before the class path. 在课程路径之前。 From tutorial you linked: 从教程中您链接:

You must be sure to place a dot (.) in the first position of the class path. 您必须确保在类路径的第一个位置放置一个点(。)。 If it not, you get a message stating that Java cannot load the class. 如果不是,您将收到一条消息,指出Java无法加载该类。

Also check if the matlab compiler path ("c:\\Program Files\\MATLAB\\MATLAB Compiler Runtime\\v82\\toolbox\\javabuilder\\jar\\javabuilder.jar" - in the tutorial) is correct for your system. 还要检查matlab编译器路径(本教程中的“ c:\\ Program Files \\ MATLAB \\ MATLAB编译器运行时\\ v82 \\ toolbox \\ javabuilder \\ jar \\ javabuilder.jar”)是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM