Change Directory in MATLAB from Terminal/JAVA

2019-08-20 17:48发布

I need to be able to change the working directory in MATLAB without interacting with the command window. I'm launching MATLAB from a Java application. Right now the only solution I've come up with is closing MATLAB, changing directory from JAVA and relaunching. Is there some streamlined way to send MATLAB the 'cd' command from JAVA? Doing so from the command-line would also work, since I could use getRuntime().exec(command)

Thanks!

2条回答
ら.Afraid
2楼-- · 2019-08-20 18:20

You can do this using JMI if you're using the same JVM as Matlab (if not then I have no idea). There is not much online info about this (it's WAY undocumented/unsupported). Google it or read this: http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html. In short, you need to include Matlab's relevant JAR file and then use com.mathworks.jmi.Matlab's functionality. For example:

Matlab.evalConsoleOutput("cd('C:\Program Files\')");

Yair Altman http://UndocumentedMatlab.com

查看更多
可以哭但决不认输i
3楼-- · 2019-08-20 18:23

I am working with Stephen Poletto who posted the original question. There wasn't any existing solution that met our needs so we wrote our own solution based off of Kamin Whitehouse's work mentioned by Yair. It is available for all to use at matlabcontrol.googlecode.com

It allows for controlling MATLAB from a Java program launched outside of MATLAB.

查看更多
登录 后发表回答