Main Content

Run Simulink Simulation from Java

MATLAB Command to Run Simulation

You can run Simulink® simulations using the MATLAB® Engine API for Java®. Here are the basic steps to run a simulation programmatically.

  • Create a MATLAB engine object and start a MATLAB session.

  • Load the Simulink model in MATLAB (load_system (Simulink)).

  • Run the simulation with specific simulation parameters (sim (Simulink)).

  • Access the results of the simulation using methods of the returned Simulink.SimulationOuput (Simulink) object.

For information on running simulations programmatically from MATLAB, see Run Individual Simulations (Simulink).

Run vdp Model from Java

The Simulink vdp block diagram simulates the van der Pol equation, which is a second order differential equation. The model solves the equations using the initial conditions and configuration parameters defined by the model.

MATLAB Code to Run Simulation

This MATLAB code shows the commands to run the simulation programmatically. The Simulink.SimulationOuput object get method returns the results and time vector.

mdl = 'vdp';
simOut = sim(mdl,'SaveOutput','on',...
y = simOut.get('yOut');
t = simOut.get('tOut');

Graph the Data

This MATLAB code creates a graph of the simulation output and exports the graph to a JPEG image file.


Java Code to Run Simulation

This Java code runs the Simulink vdp model simulation and returns the results to Java. The implementation performs these operations:

  • Creates a MATLAB engine object and start a MATLAB session.

  • Calls the MATLAB load_system command to start Simulink and load the vdp model asynchronously. Poll the task until the Future returns.

  • Calls the MATLAB sim command to set simulation parameters and run the simulation. Poll the task until the Future returns.

  • Captures the result of the simulation. The output of the sim function is a MATLAB Simulink.SimulationOuput (Simulink) object, which is created in the MATLAB base workspace.

    The engine API does not support this type of object. Therefore, this example uses the object get method to access the simulation data in the MATLAB workspace.

  • Creates a graphic of the simulation data and exports this graph to a JPEG file.

  • Returns the simulation results and time vector to Java as double arrays.

import com.mathworks.engine.*;
import java.util.concurrent.Future;
import java.util.Arrays;

public class RunSimulation {
    public static void main(String[] args) throws Exception {
        MatlabEngine eng = MatlabEngine.startMatlab();
        Future<Void>  fLoad = eng.evalAsync("load_system('vdp')");
        while (!fLoad.isDone()){
            System.out.println("Loading Simulink model...");
        Future<Void> fSim = eng.evalAsync("simOut = sim('vdp','SaveOutput'," +
                "'on','OutputSaveName','yOut'," +
        while (!fSim.isDone()) {
            System.out.println("Running Simulation...");
        // Get simulation data
        eng.eval("y = simOut.get('yOut');");
        eng.eval("t = simOut.get('tOut');");
        // Graph results and create image file
        // Return results to Java
        double[][] y = eng.getVariable("y");
        double[] t = eng.getVariable("t");
        // Display results
        System.out.println("Simulation result " + Arrays.deepToString(y));
        System.out.println("Time vector " + Arrays.toString(t));

Related Topics