简体   繁体   中英

using matlabcontrol API to call to call matlab function from Java within Netbeans

I have been trying to edit the the following matlabcontrol code but still there is an error when I run it. Please friends help me out!

package matcontro;

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:\\ Users\\HASENDE\\My Documents\\MATLAB')");
         proxy.feval("myfunc");
         proxy.eval("rmpath('C:\\ Users\\HASENDE\\My Documents\\MATLAB')");

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

The error that I get is below;

run:

Exception in thread "main" matlabcontrol.MatlabConnectionException: Could not launch MATLAB. Command: [matlab, -r, javaaddpath 'C:\\Users\\HASENDE\\Documents\\NetBeansProjects\\Java Classpath Libraries\\matlabcontrol-4.0.0.jar'; matlabcontrol.MatlabClassLoaderHelper.configureClassLoading(); javarmpath 'C:\\Users\\HASENDE\\Documents\\NetBeansProjects\\Java Classpath Libraries\\matlabcontrol-4.0.0.jar'; matlabcontrol.MatlabConnector.connectFromMatlab('PROXY_RECEIVER_01caa56d-9ed7-4e39-a45b-345051024d49', 2100);]

at matlabcontrol.RemoteMatlabProxyFactory.createProcess(RemoteMatlabProxyFactory.java:305) at matlabcontrol.RemoteMatlabProxyFactory.requestProxy(RemoteMatlabProxyFactory.java:116)

at matlabcontrol.RemoteMatlabProxyFactory.getProxy(RemoteMatlabProxyFactory.java:134)

  at matlabcontrol.MatlabProxyFactory.getProxy(MatlabProxyFactory.java:81) at matcontro.HelloWorld.main(HelloWorld.java:21) 

Caused by: java.io.IOException: Cannot run program "matlab": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) at matlabcontrol.RemoteMatlabProxyFactory.createProcess(RemoteMatlabProxyFactory.java:292) ... 4 more

Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified

  at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>(ProcessImpl.java:188) at java.lang.ProcessImpl.start(ProcessImpl.java:132) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021) ... 5 more 

Java Result: 1

BUILD SUCCESSFUL (total time: 4 seconds)

The issue is that matlabcontrol on Windows and Linux expects 'matlab' to be understood due to the MATLAB directory being part of your PATH environment variable. This exception is indicating that is not the case. That's fine, you just need to explicitly set the location of where your MATLAB executable is. From the javadoc for setMatlabLocation(...) :

Sets the location of the MATLAB executable or script that will launch MATLAB. If the value set cannot be successfully used to launch MATLAB, an exception will be thrown when attempting to create a proxy.

The absolute path to the MATLAB executable can be determined by running MATLAB. On OS X or Linux, evaluate [matlabroot '/bin/matlab'] in the Command Window. On Windows, evaluate [matlabroot '/bin/matlab.exe'] in the Command Window. The location provided does not have to be an absolute path so long as the operating system can resolve the path.

Windows

Locations relative to the following will be understood:

  • The current working directory
  • The Windows directory only (no subdirectories are searched)
  • The Windows\\System32 directory
  • Directories listed in the PATH environment variable
  • App Paths defined in the registry with key HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths

By default on Windows, MATLAB places an App Path entry in the registry so that matlab can be used to launch MATLAB. If this property is not set, this App Path entry will be used.

OS X

Locations relative to the following will be understood:

  • The current working directory
  • Directories listed in the PATH environment variable

On OS X, MATLAB is installed in /Applications/ as an application bundle. If this property is not set, the executable inside of the application bundle will be used.

Linux

Locations relative to the following will be understood:

  • The current working directory
  • Directories listed in the PATH environment variable

During the installation process on Linux, MATLAB can create a symbolic link named matlab that can be used to launch MATLAB. If this property is not set, this symbolic link will be used.

Just to complement the answer, I had a similar problem (I am using Intellij IDEA and Matlab R2014a). Indeed the exact path of the program was missing from the Enviromental Variable Path.Some matlab paths can be found (or automatically written when installing matlab) like "C:\\Program Files\\MATLAB\\MATLAB Runtime\\" or "C:\\Program Files\\MATLAB\\MATLAB Compiler\\" but only the one that holds the .exe work, like "C:\\Program Files\\MATLAB\\R2014a\\bin". Yet, my program didn't work till I re-start the IDE. Keep that in mind.

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