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).
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.
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'; load_system(mdl); simOut = sim(mdl,'SaveOutput','on',... 'OutputSaveName','yOut',... 'SaveTime','on',... 'TimeSaveName','tOut'); y = simOut.get('yOut'); t = simOut.get('tOut');
This MATLAB code creates a graph of the simulation output and exports the graph to a JPEG image file.
plot(t,y) print('vdpPlot','-djpeg')
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..."); Thread.sleep(10000); } Future<Void> fSim = eng.evalAsync("simOut = sim('vdp','SaveOutput'," + "'on','OutputSaveName','yOut'," + "'SaveTime','on','TimeSaveName','tOut');"); while (!fSim.isDone()) { System.out.println("Running Simulation..."); Thread.sleep(10000); } // Get simulation data eng.eval("y = simOut.get('yOut');"); eng.eval("t = simOut.get('tOut');"); // Graph results and create image file eng.eval("plot(t,y)"); eng.eval("print('vdpPlot','-djpeg')"); // 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)); eng.close(); } }