简体   繁体   中英

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

I have 2 .m files. One is the function and the other one (read.m) reads then function and exports the results into an excel file. I have a java program that makes some changes to the .m files. After the changes I want to automate the execution/running of the .m files. 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. 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.

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 ). That could be done quite easily (with joining Files.readAllLines() for example).

Hope that helps something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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