从 Java 运行 MATLAB 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1607933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running MATLAB function from Java
提问by Tom
I have an .m file in MATLAB which I would like to call from Java an get the solution as a string or whatever in Java. This sounds really simple but for some reason I can't make it work.
我在 MATLAB 中有一个 .m 文件,我想从 Java 调用它并以字符串或 Java 中的任何内容获取解决方案。这听起来很简单,但由于某种原因我无法让它工作。
I tried this:
我试过这个:
matlab -nosplash -wait -nodesktop -r myFunction
but I'm not sure how I parse the answer since MATLAB opens it's own command line (in Windows).
但我不确定如何解析答案,因为 MATLAB 打开它自己的命令行(在 Windows 中)。
I use this, but it doesn't return anything.
我使用这个,但它不返回任何东西。
Process p = Runtime.getRuntime().exec(commandToRun);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
also it seems that every time I call MATLAB it opens a separate window which is a problem because I'd like to run this many times.
似乎每次我调用 MATLAB 时它都会打开一个单独的窗口,这是一个问题,因为我想多次运行。
回答by Jeff Storey
The trick is to use the MatlabControl class http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html. It's very easy to use and you can do exactly what you're trying to do (and more).
诀窍是使用 MatlabControl 类http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html。它非常易于使用,您可以完全按照自己的意愿去做(甚至更多)。
回答by Wolfgang Ulmer
There exists a good Java-COM-Bridge called JaCoB (http://sourceforge.net/projects/jacob-project/) which you can use to automatically start Matlab as a COM-Server in the background. You can then follow the instructions in the Matlab help to interact with the Matlab COM Interface.
有一个很好的 Java-COM-Bridge 称为 JaCoB ( http://sourceforge.net/projects/jacob-project/),您可以使用它在后台自动启动 Matlab 作为 COM-Server。然后您可以按照 Matlab 帮助中的说明与 Matlab COM 接口进行交互。
Although this is a very generic interface, it provides enough flexibility to easily do a few calls to Matlab like in your case.
尽管这是一个非常通用的接口,但它提供了足够的灵活性,可以像您的情况一样轻松地对 Matlab 进行几次调用。
Simply download the JaCoB package and look in the docs folder for some documentation. You also have to include the Jacob DLL in your path.
只需下载 JaCoB 包并在 docs 文件夹中查找一些文档。您还必须在路径中包含 Jacob DLL。
回答by Joshua Kaplan
matlabcontrolis based on the same underlying MATLAB library used by MatlabControl mentioned by Jeff, but is more up to date, reliable, and documented. To get started, take a look at the walkthrough.
matlabcontrol基于 Jeff 提到的MatlabControl使用的相同底层 MATLAB 库,但更新、可靠和记录。首先,请查看演练。
回答by Maksim Khadkevich
回答by quepas
In Matlab R2016b, MathWorks added MATLAB Engine API for Javawhich allows to execute MATLAB code from Java.
在 Matlab R2016b 中,MathWorks为 Java添加了MATLAB Engine API,允许从 Java 执行 MATLAB 代码。