I understood that you’re creating a workflow where MATLAB runs a simulation in COMSOL via LiveLink, writes an XML, then runs a Java program (from Eclipse) to process that XML, and then loops back. MATLAB can call Java code directly, assuming you compile your Java code (from Eclipse) into a .jar. Once compiled, MATLAB can:
- Load the JAR
- Instantiate Java classes
- Call methods
- Loop back into the COMSOL simulation
This makes your for loop control fully centralized inside MATLAB.
Follow the below mentioned steps to run Java code from MATLAB:
- Compile Java Program to JAR
In Eclipse:
- Right-click your project > Export > Java > JAR file
- Export the compiled class into MyProcessor.jar
Make sure your main class looks like:
public class MyProcessor {
public static void process(String xmlFilePath) {
System.out.println("Processing " + xmlFilePath);
2. Load JAR into MATLAB
Place the .jar in MATLAB path or current working directory.
javaaddpath('MyProcessor.jar');
Call the method like this:
javaMethod('process', 'MyProcessor', 'C:/path/to/myInput.xml');
or if it's a static method:
MyProcessor.process('C:/path/to/myInput.xml');
The final full control loop looks like this:
javaaddpath('MyProcessor.jar');
model = mphopen('yourModel.mph');
exportXML(model, sprintf('run_%d.xml', i));
MyProcessor.process(sprintf('C:/path/run_%d.xml', i));
Note: If your Java code relies on additional libraries, or you don’t want to tightly couple it with MATLAB’s JVM, use the following approach:
cmd = 'java -cp "C:\path\to\MyProcessor.jar" MyProcessor C:\path\to\input.xml';
[status, cmdout] = system(cmd);
I hope this helps achieve the desired functionality.