Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Code licensed under new-style BSD (see LICENSE).
* All code up to tags/original: Copyright (c) 2013, Joshua Kaplan
* All code after tags/original: Copyright (c) 2016, DiffPlug
*/
package matlabcontrol;
import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicReference;
import matlabcontrol.MatlabProxy.MatlabThreadCallable;
import matlabcontrol.MatlabProxy.MatlabThreadProxy;
import com.mathworks.jmi.Matlab;
import com.mathworks.jmi.NativeMatlab;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Interacts with MATLAB via the undocumented Java MATLAB Interface (JMI).
*
* This code is inspired by Kamin Whitehouse's
* MatlabControl. Fixes to concurrency
* bugs in this class have been aided by the feedback of several matlabcontrol users, thank you for your feedback!
*
* This class runs inside of MATLAB's Java Virtual Machine and relies upon the Java MATLAB Interface which is
* distributed by MathWorks as {@code jmi.jar}. It allows for Java to send {@code eval} and {@code feval} statements to
* MATLAB and receive results. {@code jmi.jar} is not distributed with matlabcontrol as it is the property of
* MathWorks. If you wish to compile the source code you will need to reference the version of {@code jmi.jar} that is
* distributed with your copy of MATLAB. It is located at {@code matlabroot/java/jar/jmi.jar} where {@code matlabroot}
* is the location of your MATLAB installation. The location of {@code matlabroot} can be determined by executing the
* {@code matlabroot} command in the MATLAB Command Window.
*
* This is the only class in matlabcontrol which directly links against code in {@code jmi.jar}. (And therefore also the
* only class that needs {@code jmi.jar} to be on the classpath in order to compile.) {@link Configuration} also uses
* code in {@code jmi.jar} but uses reflection to interact with it.
*
* @since 3.0.0
*
* @author Joshua Kaplan
*/
class JMIWrapper {
private static final MatlabThreadOperations THREAD_OPERATIONS = new MatlabThreadOperations();
private static final EventQueue EVENT_QUEUE = Toolkit.getDefaultToolkit().getSystemEventQueue();
private static final Method EVENT_QUEUE_DISPATCH_METHOD;
static {
try {
EVENT_QUEUE_DISPATCH_METHOD = EventQueue.class.getDeclaredMethod("dispatchEvent", AWTEvent.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("java.awt.EventQueue's protected void dispatchEvent(java.awt.AWTEvent) " +
"method could not be found", e);
}
EVENT_QUEUE_DISPATCH_METHOD.setAccessible(true);
}
private JMIWrapper() {}
/**
* Exits MATLAB without waiting for MATLAB to return, because MATLAB will not return when exiting.
*
* @throws MatlabInvocationException
*/
@SuppressFBWarnings(value = "DLS_DEAD_LOCAL_STORE", justification = "False positive.")
static void exit() {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Matlab.mtFevalConsoleOutput("exit", null, 0);
}
//This should never fail, and if it does there is no way to consistently report it back to the caller
//because this method does not block
catch (Exception e) {
// make as much noise as we can
e.printStackTrace();
throw new RuntimeException(e);
}
}
};
if (NativeMatlab.nativeIsMatlabThread()) {
runnable.run();
} else {
Matlab.whenMatlabIdle(runnable);
}
}
//The following functions wait for MATLAB to complete the computation before returning
//See MatlabProxy for the method documentation, acts as if running inside MATLAB
//(A LocalMatlabProxy is just a thin wrapper around these methods)
static void setVariable(final String variableName, final Object value) throws MatlabInvocationException {
invokeAndWait(new MatlabThreadCallable() {
@Override
public Void call(MatlabThreadProxy proxy) throws MatlabInvocationException {
proxy.setVariable(variableName, value);
return null;
}
});
}
static Object getVariable(final String variableName) throws MatlabInvocationException {
return invokeAndWait(new MatlabThreadCallable