如何使用 matlabcontrol.jar 从 Java 调用用户定义的 Matlab

发布于 2024-12-01 19:30:01 字数 530 浏览 0 评论 0原文

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

然后我尝试使用一个简单的用户定义函数,该函数不带参数并且只有单行 disp('Hello') 但结果仍然相同。因此,我认为用户定义函数的调用方式存在问题,而不是类型转换问题。

请有人能尽快帮助我吗?我很快就会达到这个项目的最后期限。如果有人能提出解决方案,我将非常感激。 (Joshuwa Kaplan 先生,您的帖子中是否有解决此类问题的指南?我尝试过,但一无所获)

提前致谢

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.

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.

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)

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极度宠爱 2024-12-08 19:30:01

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

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

C:\some\path\myfunc.m

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 程序:

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

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

hello world
hello from MYFUNC

您还可以将文件夹添加到路径一次,然后使用 SAVEPATH 保存它。这样您就不必每次都这样做。

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:

C:\some\path\myfunc.m

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

We compile and run the Java program:

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

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

hello world
hello from MYFUNC

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文