如何使用 matlabcontrol.jar 从 Java 调用用户定义的 Matlab [英] How to call a user defined Matlab from Java using matlabcontrol.jar

查看:24
本文介绍了如何使用 matlabcontrol.jar 从 Java 调用用户定义的 Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用一个用户定义的 Matlab 函数(M 文件),它从我在 Eclipse 中开发的 Java 应用程序中获取 3 个参数(Java 字符串).目前,我可以使用 dispsqr<等函数/命令调用 proxy.evalproxy.feval 方法/代码>.但是当我尝试调用用户定义的函数时,它在 matlab 控制台上说没有这样定义的函数,并且在 Java 控制台上发生 MatlabInvocationException.

I am trying to call a user defined Matlab Function(M file) which takes 3 arguments(Java Strings) from my Java application which is developed in Eclipse. At the moment I am able to call proxy.eval and proxy.feval methods with the functions/commands like disp or sqr. But when i try to invoke a user-defined function it says on the matlab console that there is no such function defined like that and on the Java console MatlabInvocationException occurs.

然后我尝试了一个简单的用户定义函数,它不带参数,只有一行 disp('Hello') 但结果仍然相同.所以我认为用户定义函数的调用方式有问题,而不是类型转换问题.

Then I tried with a simple user-defined function which takes no arguments and just has single line disp('Hello') but still the result is same. So I think rather than a type conversion problem there is something wrong with how user-defined functions are getting invoked.

请问有人能尽快帮我吗?我很快就要赶上这个项目的最后期限了.如果有人能提出解决方案,我将不胜感激.(Joshuwa Kaplan 先生,您的帖子中是否有解决此类问题的指南?我试过了,但一无所获)

Please can anyone help me soon? I am meeting the deadline very soon for this project. I would be so thankful if someone can come up with a solution. (Mr Joshuwa Kaplan, is there any guide on solving an issue like this in your posts? I tried but found nothing)

提前致谢

推荐答案

MATLAB 搜索路径上必须有任何用户定义的 m 文件,就像在 MATLAB 中正常工作一样.

You must have any user-defined m-files on the MATLAB search path, just as if you were working normally inside MATLAB.

我使用以下示例进行了测试:

I tested with the following example:

function myfunc()
    disp('hello from MYFUNC')
end

HelloWorld.java

import matlabcontrol.*;

public class HelloWorld
{
    public static void main(String[] args)
        throws MatlabConnectionException, MatlabInvocationException
    {
         // create proxy
         MatlabProxyFactoryOptions options =
            new MatlabProxyFactoryOptions.Builder()
                .setUsePreviouslyControlledSession(true)
                .build();
        MatlabProxyFactory factory = new MatlabProxyFactory(options);
        MatlabProxy proxy = factory.getProxy();

        // call builtin function
        proxy.eval("disp('hello world')");

        // call user-defined function (must be on the path)
        proxy.eval("addpath('C:\some\path')");
        proxy.feval("myfunc");
        proxy.eval("rmpath('C:\some\path')");

        // close connection
        proxy.disconnect();
    }
}

我们编译并运行Java程序:

We compile and run the Java program:

javac -cp matlabcontrol-4.0.0.jar HelloWorld.java
java -cp ".;matlabcontrol-4.0.0.jar" HelloWorld

一个 MATLAB 会话将打开,并显示输出:

a MATLAB session will open up, and display the output:

hello world
hello from MYFUNC

您也可以将您的文件夹添加到路径中一次,然后使用 SAVEPATH 将其保留.这样您就不必每次都这样做.

You could also add your folder to the path once, then persist it using SAVEPATH. That way you won't have to do it each time.

这篇关于如何使用 matlabcontrol.jar 从 Java 调用用户定义的 Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆