This example shows the basics of how to create a model, add blocks to it, connect blocks, and simulate the model. You also learn how to organize your model with subsystems, name parts of a model, and modify a model.
For a summary of mouse actions to connect blocks and add ports interactively, see Block and Signal Line Shortcuts and Actions.
In the Simulink® Start Page, click the Blank Model template.
A new model based on the template opens in the Simulink Editor.
Open the Library Browser so that you can access the blocks you need to add to your model. In the Simulink Editor, click the Library Browser button .
A minimal model takes an input signal, operates on it, and outputs the result. In the Library Browser, the Sources library contains blocks that represent input signals. The Sinks library has blocks for capturing and displaying outputs. The other libraries contain blocks you can use for a variety of purposes, such as math operations.
In this example of a basic model, the input is a sine wave, the operation is a gain (which increases the signal value by multiplying), and you output the result to a scope. Try different techniques to explore the library and to add blocks to your model.
Open the Sources library. In the tree view of the Library Browser, click the Sources library.
In the right pane, hover over the Sine Wave block to see a tooltip describing its purpose.
Add a block to your model. Right-click the Sine Wave block and select Add block to model untitled. To learn more about the block, right-click the block and select Help.
Add a block to your model by dragging. In the library tree view, click Math Operations. In the Math Operations library, locate the Gain block and drag it to your model to the right of the Sine Wave block.
In the library tree view, click Simulink to view the sublibraries as icons in the right pane. This view is an alternative way to navigate the library structure. Double-click the Sinks library icon.
In the Sinks library, locate the Scope block and add it to your model using the context menu or by dragging it.
The figure shows your model so far.
The editor names blocks as you add them. For example, it names the first Gain block that you add Gain, the next Gain1, and so on. By default, the Simulink Editor hides these names. However, you can see the name by selecting the block. You can also explicitly name a block so that the name appears. You can display all names given by the editor. In the Debug tab, select Information Overlays > Hide Automatic Block Names. For more information on displaying block names, see Manage Block Names.
Connect the blocks to create the relationships between model elements that are needed to make the model operate. Reading the model is easier when you line up blocks according to how they interact with each other. Shortcuts help you to align and connect blocks.
Drag a Gain block so it lines up with a Sine Wave block. An alignment guide appears when the blocks line up horizontally.
Release the block, and a blue arrow appears as a preview of the suggested connection.
To make the connection, click the arrow. A solid line appears in place of the guide.
Line up and connect the Scope block to the Gain block using the same technique. Additional guides appear when multiple blocks are within range.
For additional alignment options, on the Format tab, click options in the Align section.
You can set parameters on most blocks. Parameters help you to specify how a block operates in the model. You can use the default values or you can set values. Use the Property Inspector to set parameters. Alternatively, you can double-click most blocks to set the parameters using a block dialog box. To understand when to use each approach, see Setting Properties and Parameters.
In your model, set the sine wave amplitude and the gain value.
Display the Property Inspector. On the Modeling tab, under Design, click Property Inspector.
Select the Sine Wave block.
In the Property Inspector, set the Amplitude parameter to
For blocks whose value appears on the icon, you can edit the parameter interactively. Select the Gain block. Hover over the block. A blue underline appears under the number.
Set the Gain parameter to
3. Click the
underlined number, delete it, and type
In a block dialog box or in the Property Inspector, when you set a block parameter value to a variable or function, Simulink provides a suggested list to select from based on the current text typed in the edit field. The suggestions include relevant variables or objects from every workspace (base, model, and mask), data dictionary, and referenced dictionary visible to the editable block parameter. Autocomplete is available for variables, fields of structures and objects, and functions on the MATLAB path.
Suppose that you want to perform another gain but on the absolute value of the output from the Sine Wave block. Add blocks for this purpose, trying different techniques for locating blocks in the library and adding them to your model.
If you know the name of the block that you want to add, you can use a shortcut. Double-click where you want to add the block, and type the block name, in this case Gain.
The list of suggestions shown are dynamically ranked based on your recent block usage history.
Click the block name or, with the block name highlighted, press Enter. You can use the arrow keys to highlight the block name if it is not first in the list.
Some blocks display a prompt for you to enter a value for one of the block
parameters. The Gain block prompts you to enter the
Gain value. Type
3 and press
To perform an absolute value, add an Abs block. Suppose you do not
know the library a block is in or the full name of the block. You can search for it
using the search box in the Library Browser. Enter
abs in the
search box and press Enter. When you find the Abs block, add it to
the left of the new Gain block.
Add another Scope block. You can right-click the existing Scope block and drag to create the copy or use the Copy and Paste commands.
The figure shows the current state of the model.
The input to the second Gain block is the absolute value of the output from the Sine Wave block. To use a single Sine Wave block as the input to both gain operations, create a branch from the Sine Wave block output signal.
For the first set of blocks in your model, you used the horizontal alignment guides to help you align and connect them. You can also use guides to align blocks vertically. Drag the second Scope block so that it lines up under the first one. Release it when the vertical alignment guide shows that the blocks are aligned.
You can click two ports to connect them. After you click the first port, compatible ports appear highlighted. Click another port to connect.
Align and connect the blocks as shown.
Create a branch from the Sine Wave block output to the Abs block. Click the input port of the Abs block. Move the cursor toward the output signal line from the Sine Wave block. A preview line appears. Click to create the branch.
Alternatively, you can start the branch by clicking the line segment and then moving the cursor toward the port.
Name signals. Double-click the signal between the lower Gain block and the Scope
block and type
Scope. Double-click the line and not a blank area of
the canvas. For other techniques that you can use with signal names, see Signal Name and Label Actions.
Try these methods to add or connect blocks:
Drag from a block port and release so that a red, dotted line appears. Double-click the end of the line to use the block insertion shortcut. Suggested blocks for the current context appear on the menu. You can select one of the listed blocks.
To improve the menu suggestions based on your model designs, see Improve Quick Block Insert Results.
Double-click and then type the name of a block to get a list of blocks that starts with the characters you typed. For custom library blocks, you can type the block keyword if the library author assigned one. The list is ranked based on your recent block-usage history.
After you click a port, hold Shift as you connect to another port. Holding Shift puts you in a mode in which you can make multiple, consecutive connections. For example, while holding Shift, you can branch a new signal line and connect it to another port or signal line with one click.
Select the first block and Ctrl+click the block you want to connect it to. This technique is useful when you want to connect blocks that have multiple inputs and outputs, such as multiple blocks to a bus or two subsystems with multiple ports. As with clicking two ports, this technique is useful when you do not want to align blocks. The signal line bends as needed to make the connection.
To approximate a diagonal line from line segments, press Shift and drag a vertex.
To improve the shape of a signal line, select the line and, from the action bar, select Auto-route Line. The line redraws if a better route between model elements is possible. You can select Auto-route Lines from the action bar to improve lines with a single block selected or with multiple model elements selected by dragging a selection box.
You can group blocks in subsystems and label blocks, subsystems, and signals. For more information about subsystems, see Create a Subsystem.
Drag a selection box around the Abs block and the Gain block next to it.
Move the cursor over the ellipses that appear at the corner of the box where you ended the selection. From the action bar, select Create Subsystem.
A subsystem block appears in the model in place of the selected blocks.
To resize the subsystem block for the best fit in your model, drag the block handles.
Give the subsystem a meaningful name. Select the block, double-click the name, and
Absolute Value. Naming a block causes the name to appear in
Open the Absolute Value subsystem by double-clicking it.
To use the Explorer Bar to navigate the model hierarchy, right-click the subsystem and select Open in New Tab.
The subsystem contains the blocks and signal that you selected as the basis of the subsystem. They are connected in sequence to two new blocks: an Inport block and an Outport block. Inport and Outport blocks correspond to the input and output ports on the subsystem. Creating the subsystem from a selection that includes a named signal adds the name of the signal to the corresponding Inport or Outport block.
Click the Up to Parent button to return to the top level of the model.
The figure shows the model after you create the subsystem and name it.
You can simulate a model by clicking the Run button .
In this example, simulation runs for 10 seconds, the default setting.
Double-click both Scope blocks to open them and view the results.
The figure shows the two results. In the second plot, as expected, the absolute value of the sine wave is always positive.
You can add blocks to a signal, remove blocks from models, and redraw connections. To modify this model, add a bias to the input to both branches of your model. Also, replace one of the scopes with a different sink. Add more blocks to the subsystem and another output.
Add a Bias block to the model and set the Bias
Drag the block onto the signal line after the Sine Wave block but before the branch. If you need to make room for the block, drag the Sine Wave block to the left or move the end of the branch by dragging it to the right.
When you drag the block onto the signal line, the block connects to the signal line at both ends. Release the block when you are satisfied with the position.
Remove the top Scope block. If you want to disconnect it from the model, but do not want to delete it, press and hold Shift and drag the block. Cut or delete it using Ctrl-X or the Delete key. The broken connection appears as a red dotted line.
When you delete a block that has one input and one output, a prompt appears between the broken connection lines. Click the prompt to connect the signals.
Add a To Workspace block to the model at the end of the broken connection. The To Workspace block outputs the results to a variable in the MATLAB workspace.
Add a Sine Wave block to the model and set the amplitude to
5. Place it to the left of the subsystem.
Add another input to the subsystem. Drag a line from the new Sine Wave block to the left side of the subsystem. A new port, In2, appears on the subsystem.
For certain blocks, dragging a line to it adds an input port or output port. For example, a port appears on a subsystem when you connect a line to it. Other blocks that add ports include the Bus Creator, Scope, and Add, Sum, and Product blocks.
Add an output to the subsystem. Add another To Workspace block to the model and place it to the right of the subsystem. Drag a line from its input port to the right side of the subsystem. A new port, Out2, appears on the subsystem.
Open the subsystem and rename the Out2 block Workspace. Add a Manual Switch block to the subsystem. Resize it and connect it as shown. Branch the signal after the Gain block to direct the output to the To Workspace block.
Then, return to the top level of the model. The figure shows the current model.
Simulate the model.
appear in the MATLAB workspace. Double-click each variable to explore the results.
If you want to use the second sine wave as input to the subsystem algorithm, open the subsystem and double-click the switch. The input changes to In2. Simulate again.
To toggle between simulating the model with and without the effects of the Bias block, right-click the Bias block and select Comment Through. The block stays in the model but does not affect the operation. Right-click the Bias block and select Uncomment to enable the block. The Comment Out command comments out the block’s output signal, so signal data does not pass through. Try each of these commands to better understand their effects.