Introduction
Matlab is a programming and numerical computing environment widely used in academic and industrial settings, especially for solving engineering and mathematics problems. In professional development and academic research contexts, it could be useful to delegate some calculations to Matlab from Java. In this article, we will explore how to integrate and use Matlab in a Java application, thus allowing you to harness the powerful features of Matlab directly within your Java code.
Installing the Correct Version of Matlab
Before proceeding with the integration of the library in Java, it is necessary to ensure that Matlab is installed on your computer. The Matlab release must be equal to or higher than version R2016B. Subsequently, it is important to find, and possibly download, the necessary library for integration with Java. MATLAB Java Package (JAR) contains the Java classes required for the proper integration. It can be found in the Matlab installation folder, under the path: <matlabroot>/extern/engines/java. If you cannot locate the engine.jar file, you can download it from this link. Please note that the package is related to version R2023A.
Integrating the Library in Java
After installing the required packages in Matlab, you can proceed with the integration of the Java library. Import the MATLAB Java Package (JAR) file into your Java project. In most Java development environments, it is sufficient to right-click on the project and select “Import JAR” or “Add External JARs”, then locate the previously mentioned JAR file. The following source code demonstrates how to integrate and use Matlab in Java.
Example: How to use Matlab in a Java application
import com.mathworks.engine.*;
public class MatlabExample {
public static void main(String[] args) {
try {
// Avvia una sessione Matlab
MatlabEngine engine = MatlabEngine.startMatlab();
// Definisci un array di input
double[] inputArray = {1, 2, 3, 4, 5};
// Calcola la somma degli elementi dell'array utilizzando una funzione Matlab
double sum = engine.feval("sum", inputArray);
// Stampa il risultato
System.out.println("La somma degli elementi dell'array è: " + sum);
// Calcola il prodotto degli elementi dell'array utilizzando una funzione Matlab
double product = engine.feval("prod", inputArray);
// Stampa il risultato
System.out.println("Il prodotto degli elementi dell'array è: " + product);
// Esegui una funzione Matlab personalizzata
// Assumendo che la funzione "myFunction.m" esista nel percorso corrente di Matlab
double[] outputArray = engine.feval("myFunction", inputArray);
// Stampa il risultato
System.out.println("L'output della funzione personalizzata è:");
for (double element : outputArray) {
System.out.print(element + " ");
}
// Chiudi la sessione Matlab
engine.close();
} catch (Exception e) {
System.err.println("Errore durante l'esecuzione di Matlab in Java: " + e.getMessage());
}
}
}
Conclusion
In this example, we started a Matlab session within the Java application and used some predefined Matlab functions (sum
and prod
) to perform operations on the elements of an array. We also called a custom Matlab function named “myFunction.m” and printed the result. Finally, we closed the Matlab session to free up resources.