Is it possible to make MatlabEngine class object persistent in Java?
Dear Matlab specialists!
My colleague wrote a simple Java class (code below) to create and use MatlabEngine "eng" object in the run() function. It works normally.
However, when one makes this object a member of class and then tries to reuse it, it looks like the whole associated Matlab session disappears after the first usage.
I am intersted in creating MatlabEngine object/session "eng" in a way when it is possible to call it from other Java classes for which the Java class where it is created, is visible. In particular, I’d like to create a Matlab class object within "eng", make some setups (which need quite long calculations), and then call the "Do(current_data)" function of this created Matlab class on my current data, i.e. whenever/wherever needed in Java. For that obviously, this "eng" object and its session should be persistent. So my question is – is it posibble at all?
Best wishes,
Y.
import com.mathworks.engine.EngineException;
import com.mathworks.engine.MatlabEngine;
import com.mathworks.engine.MatlabSyntaxException;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class javaFevalFcnMulti implements Runnable{
String[] args;
int num1;
int num2;
public javaFevalFcnMulti(String[] args_in, int num1_in, int num2_in){
args = args_in;
num1 = num1_in;
num2 = num2_in;
}
@Override
public void run() {
MatlabEngine eng;
try {
System.out.println("MLT started");
eng = MatlabEngine.startMatlab();
Object[] results = eng.feval(3, "gcd", num1, num2);
Integer G = (Integer)results[0];
Integer U = (Integer)results[1];
Integer V = (Integer)results[2];
eng.close();
System.out.println("Greatest common divisor of "+Integer.toString(num1)+" and "+Integer.toString(num2)+" is "+G.toString());
} catch (EngineException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalStateException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (MatlabSyntaxException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
}
}
}Dear Matlab specialists!
My colleague wrote a simple Java class (code below) to create and use MatlabEngine "eng" object in the run() function. It works normally.
However, when one makes this object a member of class and then tries to reuse it, it looks like the whole associated Matlab session disappears after the first usage.
I am intersted in creating MatlabEngine object/session "eng" in a way when it is possible to call it from other Java classes for which the Java class where it is created, is visible. In particular, I’d like to create a Matlab class object within "eng", make some setups (which need quite long calculations), and then call the "Do(current_data)" function of this created Matlab class on my current data, i.e. whenever/wherever needed in Java. For that obviously, this "eng" object and its session should be persistent. So my question is – is it posibble at all?
Best wishes,
Y.
import com.mathworks.engine.EngineException;
import com.mathworks.engine.MatlabEngine;
import com.mathworks.engine.MatlabSyntaxException;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class javaFevalFcnMulti implements Runnable{
String[] args;
int num1;
int num2;
public javaFevalFcnMulti(String[] args_in, int num1_in, int num2_in){
args = args_in;
num1 = num1_in;
num2 = num2_in;
}
@Override
public void run() {
MatlabEngine eng;
try {
System.out.println("MLT started");
eng = MatlabEngine.startMatlab();
Object[] results = eng.feval(3, "gcd", num1, num2);
Integer G = (Integer)results[0];
Integer U = (Integer)results[1];
Integer V = (Integer)results[2];
eng.close();
System.out.println("Greatest common divisor of "+Integer.toString(num1)+" and "+Integer.toString(num2)+" is "+G.toString());
} catch (EngineException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalStateException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (MatlabSyntaxException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
}
}
} Dear Matlab specialists!
My colleague wrote a simple Java class (code below) to create and use MatlabEngine "eng" object in the run() function. It works normally.
However, when one makes this object a member of class and then tries to reuse it, it looks like the whole associated Matlab session disappears after the first usage.
I am intersted in creating MatlabEngine object/session "eng" in a way when it is possible to call it from other Java classes for which the Java class where it is created, is visible. In particular, I’d like to create a Matlab class object within "eng", make some setups (which need quite long calculations), and then call the "Do(current_data)" function of this created Matlab class on my current data, i.e. whenever/wherever needed in Java. For that obviously, this "eng" object and its session should be persistent. So my question is – is it posibble at all?
Best wishes,
Y.
import com.mathworks.engine.EngineException;
import com.mathworks.engine.MatlabEngine;
import com.mathworks.engine.MatlabSyntaxException;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class javaFevalFcnMulti implements Runnable{
String[] args;
int num1;
int num2;
public javaFevalFcnMulti(String[] args_in, int num1_in, int num2_in){
args = args_in;
num1 = num1_in;
num2 = num2_in;
}
@Override
public void run() {
MatlabEngine eng;
try {
System.out.println("MLT started");
eng = MatlabEngine.startMatlab();
Object[] results = eng.feval(3, "gcd", num1, num2);
Integer G = (Integer)results[0];
Integer U = (Integer)results[1];
Integer V = (Integer)results[2];
eng.close();
System.out.println("Greatest common divisor of "+Integer.toString(num1)+" and "+Integer.toString(num2)+" is "+G.toString());
} catch (EngineException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalStateException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (MatlabSyntaxException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(javaFevalFcnMulti.class.getName()).log(Level.SEVERE, null, ex);
}
}
} matlabengine, java MATLAB Answers — New Questions