Documentation

Build and Edit a Model in the Simulink Editor

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.

Start Simulink and Create a Model

  1. In the Simulink® Start Page, click the Blank Model template.

    A new model based on the template opens in the Simulink Editor.

  2. Open the Library Browser so that you can access the blocks you need to create your model. In the Simulink Editor, click the Library Browser button .

Add Blocks to the Model

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 that you can use to capture and display outputs. The other libraries contain blocks you can use for a variety of purposes, such as math operations.

In this 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.

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, these names are hidden. 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 by selecting Display and clearing the Hide Automatic Names check box. For more information on displaying block names, see Manage Block Names.

  1. Open the Sources library. In the tree view of the Library Browser, click the Sources library.

  2. In the right pane, hover over the Sine Wave block to see a tooltip describing its purpose.

  3. Add a block to your model using a context menu. Right-click the Sine Wave block and select Add block to model untitled. (To learn more about the block, select Help from the context menu.)

  4. 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.

  5. 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.

  6. 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.

Align and Connect Blocks

Connect the blocks to create the relationships between model elements that you need to make the model operate. Reading the model is easier when you line up the blocks according to how they interact with each other. Shortcuts help you to align and connect the blocks.

  1. Drag the Gain block so it lines up with the 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. The block name appears while the block is selected.

  2. To make the connection, click the end of the arrow. A solid line appears in place of the guide.

  3. Line up and connect the Scope block to the Gain block using the same technique.

    Tip

    Use the Diagram > Arrange menu for additional alignment options.

Set Block Parameters

You can set parameters on most blocks. Parameters help you to specify how a block operates in your model. You can use the default values or you can set values as needed. 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.

  1. Display the Property Inspector. Select View > Property Inspector.

  2. Select the Sine Wave block.

  3. In the Property Inspector, set the Amplitude parameter to 2.

  4. Select the Gain block and set the Gain parameter to 3. The value appears on the block.

Add More Blocks

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.

  1. 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. A list of possible blocks appears.

  2. 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.

  3. 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 Enter.

  4. To get 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.

  5. Add another Scope block. You can right-click the existing Scope block and drag to create the copy or use Edit > Copy and Edit > Paste.

    The figure shows the current state of your model.

Branch a Connection

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.

  1. 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.

  2. Align and connect the Abs and Gain blocks as shown.

  3. Create a branch from the Sine Wave block output to the Abs block. With your cursor over the output signal line from the Sine Wave block, press Ctrl and drag down. Drag the branch until the end is to next to the Abs block.

  4. Drag toward the Abs block until the line connects to it. Move the vertex as needed to straighten the line. (A circle appears over the vertex.)

  5. 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 connect blocks:

  • Drag a connection from the output of one block to the input of the other block. Use this technique when your blocks are already aligned, that is, no guideline appears.

  • 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. This technique is also useful when you do not want the blocks to align. The connection line bends as needed to make the connection, as shown in the figure.

    To approximate a diagonal line from line segments, press Shift and drag a vertex.

Tip

To improve the shape of a signal line, select the line and, from the ellipsis menu, select Autoroute Line. The line redraws if a better route between model elements is possible. You can select Autoroute Lines from the ellipsis menu to improve lines with a single block selected or with multiple model elements selected by dragging a selection box.

Organize Your Model

You can group blocks in subsystems and label blocks, subsystems, and signals. For more information about subsystems, see Create a Subsystem.

  1. Drag a selection box around the Abs block and the Gain block next to it.

  2. Move the cursor over the ellipses that appear at the corner of the box where you ended the selection. From the ellipsis menu, select Create Subsystem.

    A subsystem block appears in the model in place of the selected blocks. The output signal name from the Gain block becomes the name of the output port on the subsystem.

    To resize the subsystem block for the best fit in your model, drag the block handles.

  3. Give the subsystem a meaningful name. Select the block, double-click the name, and type Absolute Value. Naming a block causes the name to appear in the model.

  4. Open the Absolute Value subsystem by double-clicking it.

    Tip

    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.

  5. Click the Up to Parent button to return to the top level of the model.

  6. The figure shows the model after you create the subsystem and name it.

Simulate Model and View Results

  1. You can simulate a model using the Simulation > Run command (Ctrl+T) or the Run button . Simulate the model using the technique that you prefer.

    In this example, simulation runs for 10 seconds, the default setting.

  2. 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.

Modify the Model

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.

For some blocks, connecting 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. Additional blocks that add ports include the Bus Creator, Scope, and Add, Sum, and Product blocks. For more information, see Automatic Port Creation: Add inports and outports to blocks when routing signals​.

  1. Add a Bias block to the model and set the Bias parameter to 2.

  2. 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.

  3. Remove the top Scope block. Press Shift and drag the block if you want to disconnect it from the model but do not want to delete it. Cut or delete it using the Edit menu commands or a keystroke. The broken connection appears as a red dotted line.

    Tip

    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.

  4. 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.

  5. Add a Sine Wave block to the model and set the amplitude to 5. Place it to the left of the subsystem.

  6. 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.

  7. 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.

  8. 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.

  9. Simulate the model.

    • The simout and simout1 variables 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.

    Tip

    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.

Related Topics

Was this topic helpful?