How can I pass a variable's value from java to MATLAB's Workspace?

4 vues (au cours des 30 derniers jours)
Al Mamun
Al Mamun le 14 Déc 2017
Commenté : Al Mamun le 21 Déc 2017
Here is my Java Code. I want to pass the value of variable 'a' from java to MATLAB's Workspace. How can I do it?
public class Valuepass {
public static void main( String args[] )
{
int a=1;
System.out.println( a );
}
}

Réponses (1)

Kojiro Saito
Kojiro Saito le 19 Déc 2017
The following is procedures how to pass variable from Java to MATLAB workspace of current session.
(1) Copy MATLAB Engine jar file from $MATLAB_INSTALL\extern\engines\java\jar\engine.jar to your Java project. This will enable your java programs to import "com.mathworks.engine".
(2) Launch MATLAB and enable sharing to Java. In MATLAB Command Window, execute
matlab.engine.shareEngine
(3) Create a java file
Valuepass.java
import com.mathworks.engine.*;
public class Valuepass {
public static void main(String args[]) throws Exception {
String[] engines = MatlabEngine.findMatlab();
MatlabEngine eng = MatlabEngine.connectMatlab(engines[0]);
int a = 1;
// This will put Java variable "a" to current MATLAB workspace
eng.putVariable("a", a);
System.out.println( a );
eng.close();
}
}
(4) Build a Java file and Valuepass.jar will be created. Then, run Java,
java -jar Valuepass.jar
(5) You will find a is stored in current MATLAB workspace.
For more detail, please refer to the following documents.
  1 commentaire
Al Mamun
Al Mamun le 21 Déc 2017
Thank you for your answer. This is very much helpful.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call MATLAB from Java dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by