Executing a Simulink model directly from a script (rather than interactively) using the sim
command. You can do things like take parameters from a workspace variable, and repeatedly run sim
in a loop to simulate something while varying the parameter to see how the behavior changes, and graph the results with whatever graphical commands you like. Much easier than trying to do this interactively, and it gives you much more flexibility than the Simulink "oscilloscope" blocks when visualizing the results. (although you can't use it to see what's going on in realtime while the simulation is running)
A really important thing to know is the DstWorkspace
and SrcWorkspace
options of the simset command. These control where the "To Workspace" and "From Workspace" blocks get and put their results. Dstworkspace
defaults to the current workspace (e.g. if you call sim
from inside a function the "To Workspace" blocks will show up as variables accessible from within that same function) but SrcWorkspace
defaults to the base workspace and if you want to encapsulate your call to sim
you'll want to set SrcWorkspace
to current
so there is a clean interface to providing/retrieving simulation input parameters and outputs. For example:
function Y=run_my_sim(t,input1,params)
% runs "my_sim.mdl"
% with a From Workspace block referencing I1 as an input signal
% and parameters referenced as fields of the "params" structure
% and output retrieved from a To Workspace block with name O1.
opt = simset('SrcWorkspace','current','DstWorkspace','current');
I1 = struct('time',t,'signals',struct('values',input1,'dimensions',1));
Y = struct;
Y.t = sim('my_sim',t,opt);
Y.output1 = O1.signals.values;