简体   繁体   English

如何通过Java和Matlab控件运行.m(matlab)文件?

[英]How to run a .m (matlab) file through java and matlab control?

I have 2 .m files. 我有2个.m文件。 One is the function and the other one (read.m) reads then function and exports the results into an excel file. 一个是函数,另一个是(read.m)然后读取函数并将结果导出到excel文件中。 I have a java program that makes some changes to the .m files. 我有一个Java程序,可以对.m文件进行一些更改。 After the changes I want to automate the execution/running of the .m files. 更改后,我想自动执行/运行.m文件。 I have downloaded the matlabcontrol.jar and I am looking for a way to use it to invoke and run the read.m file that then reads the function. 我已经下载了matlabcontrol.jar,我正在寻找一种方法来使用它来调用和运行read.m文件,然后再读取该函数。 Can anyone help me with the code? 谁能帮我提供代码? Thanks 谢谢

I have tried this code but it does not work. 我已经尝试过此代码,但是它不起作用。

public static void tomatlab() throws MatlabConnectionException, MatlabInvocationException {


        MatlabProxyFactoryOptions options =
            new MatlabProxyFactoryOptions.Builder()
                .setUsePreviouslyControlledSession(true)
                .build();
        MatlabProxyFactory factory = new MatlabProxyFactory(options);
        MatlabProxy proxy = factory.getProxy();


        proxy.eval("addpath('C:\\path_to_read.m')");
        proxy.feval("read");
        proxy.eval("rmpath('C:\\path_to_read.m')");

        // close connection
        proxy.disconnect();

    }

Based on the official tutorial in the Wiki of the project , it seems quite straightforward to start with this API. 基于项目Wiki中的官方教程 ,从此API开始似乎很简单。

The path-manipulation might be a bit tricky, but I would give a try to loading the whole script into a string and passing it to eval ( please note I have no prior experience with this specific Matlab library ). 路径操作可能有点棘手,但是我会尝试将整个脚本加载到字符串中并将其传递给eval请注意,我没有使用此特定Matlab库的经验 )。 That could be done quite easily (with joining Files.readAllLines() for example). 这可以很容易地完成Files.readAllLines()例如,通过加入Files.readAllLines() )。

Hope that helps something. 希望能有所帮助。

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

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