Building Simulink Models using MATLAB Code
MATLAB has an extensive API (Application Program Interface) for building and modifying Simulink models from MATLAB code. This may be from either the MATLAB command line, from within a function or script, or from anywhere that m-code can be executed.
This tutorial discusses several of the more common functions used to build and/or manipulate a Simulink model using m-code. The primary functions for this are get_param and set_param. They enable every property of a model or block to be inspected and changed respectively from MATLAB code. Note that there are many hundreds of parameters, and for most the default settings are sufficient.
The topics covered in this tutorial are,
Other tutorials discussing Simulink and its applications for model based design are available on the Software Tutorials page.
get_param is the primary MATLAB function for inspecting the existing properties of a model.
Before using get_param the model must first be loaded into memory. This can be achieved by either opening the model manually (see Opening a New or Existing Model) or by using either of the the API functions load_system or open_system.
This tutorial uses the simpleModel developed in the tutorial Simulink Essentials - Building, Simulating and Visualizing Models, and assumes that it is has been opened.
Assuming the name of a particular parameter of interest is known then get_param can be used to inspect the specific parameter of interest. Figure 1 give some examples of using get_param. Note that some parameters do not effect the actual simulation (e.g. Name and Location) while other do (e.g. Solver and StopTime).
>> get_param('simpleModel','Name') ans = simpleModel >> get_param('simpleModel','Location') ans = 408 416 1032 616 >> get_param('simpleModel','Solver') ans = ode45 >> get_param('simpleModel','StopTime') ans = 10.0
There is also an optional input to get_param that will return a structure array containing a list all parameters and their current values. This is shown in Figure 2. Note that (as of R2010a of MATLAB) a Simulink model has 569 parameters, hence they are not all shown.
>> get_param('simpleModel','ObjectParameters') ans = Name: [1x1 struct] Tag: [1x1 struct] Description: [1x1 struct] Type: [1x1 struct] Parent: [1x1 struct] Handle: [1x1 struct] HiliteAncestors: [1x1 struct] RequirementInfo: [1x1 struct] SavedCharacterEncoding: [1x1 struct] Version: [1x1 struct]
Although not explicitly shown here, get_param is also used to inspect the parameters of blocks within a model. All blocks have a set of common parameters with each type of block having additional parameters that are specific to that block type.
The analogous function to get_param for modifying model and block parameters is called set_param. It requires three inputs: the name of the object (block, model or signal) to modify; the property to modify; and the new value. Note that some properties are read-only and hence cannot be modified.
Some examples of using set_param are given in Figure 3.
>> set_param('simpleModel','StopTime','3'); >> set_param('simpleModel','Solver','ode23'); >> set_param('simpleModel','SimulationTime','10') ??? block_diagram parameter 'SimulationTime' is read-only.
Building a Model Using M-Code
It is possible to build a Simulink model using purely MATLAB code -- without using the usual visual, point and click, mouse operations. Although rarely done, it does show how to use various of the MATLAB-Simulink API functions.
The API functions are particularly useful for developing custom blocks with functionality that include the ability to automatically modify themselves or the model in which they reside. This may be required for instance if a particular block parameter is used to configure how a block behaves during simulation.
Figure 4 gives an example of a MATLAB function that will automatically create a model. The code first checks to see if a model with the specified name already exists and if it does then it deletes it. A new model is then created using the API function new_system; the model is constructed using the API functions add_block and add_line; some model properties are modified (from their default values) using set_param; and finally the model is saved using save_system.
All of the above occurs without the model becoming visible to the user.
function autoCreateModel % function to demonstrate how to create a simple Simulink model % Author: Phil Goddard (firstname.lastname@example.org) % Specify the name of the model to create fname = 'autoCreatedModel'; % Check if the file already exists and delete it if it does if exist(fname,'file') == 4 % If it does then check whether it's open if bdIsLoaded(fname) % If it is then close it (without saving!) close_system(fname,0) end % delete the file delete([fname,'.mdl']); end % Create the system new_system(fname); % Add a Sine Wave, making the sample time continuous add_block('built-in/Sin', [gcs,'/Sine Wave'],... 'Position', [140 95 170 125],... 'SampleTime','0'); % Add a gain block, setting the gain value to 2 add_block('built-in/Gain', [gcs,'/Gain'],... 'Position',[240 95 270 125],... 'Gain','2'); % Add a scope block add_block('built-in/Scope', [gcs,'/Scope'],... 'Position',[350 94 380 126]); % Connect the sine and the gain add_line(gcs,'Sine Wave/1','Gain/1') % Connect the gain and the scope add_line(gcs,'Gain/1','Scope/1') % Set a couple of model parameters to eliminate warning messages set_param(gcs,... 'Solver','FixedStepDiscrete',... 'FixedStep','0.1'); % Save the model save_system(fname);
This tutorial has discussed topics related to using the MATLAB-Simulink API. Other Simulink tutorials are available on the Software Tutorials page.