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

Start Simulink and Create a New Model

  1. On the MATLAB® Home tab, click Simulink.

  2. In the Simulink® Start Page, click the title of the Blank Model template to expand the details, and then click Create Model.

    A new model opens in the Simulink Editor.

  3. Open the Library Browser so 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 example, 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. This example shows different techniques for exploring the library and for adding blocks to your model.

  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 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 so the model can 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.

  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 amplitude in the Sine Wave block and the value of the gain in the Gain block.

  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. Click in the model 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. You can use this technique to add blocks anywhere the blue magnifying glass appears, for example when you draw a signal line.

  3. For many blocks, a prompt appears for entering a value for one of the block parameters. The Gain block prompts you to enter the Gain value. Type 3 and press Enter.

    The name of each additional instance of a block has a number appended that increments for each new copy.

  4. Add an Abs block to the model. Suppose you do not know the library a block is in or the full name of the block. In this case, 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 model to the left of the Gain1 block.

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

    The figure shows the current state of your model.

      Note:   When you name blocks in a model in order numerically (for example Gain1, Gain2, and so on), copying and pasting them creates names that follow standard sorting order conventions for ASCII characters. This sorting order can result in a sequence of numbers on the block names that is hard to understand. To number blocks so that copying and pasting them creates names that follow a typical reading order, use a leading zero in the block names, for example Gain001, Gain002, and so on.

Branch a Connection

The input to the second gain is the absolute value of the sine wave. 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 blocks. You can also use guides to align blocks vertically. Drag the Scope1 block so that it lines up under the Scope block. Release it when the vertical alignment guide shows that the blocks are aligned.

    In this figure, the horizontal alignment guide shows that the Scope1 and Gain1 blocks are aligned.

  2. Align and connect the Abs and Gain1 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.)

Try these other 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 do not want the blocks to align. The connection line bends as needed to make the connection, as shown in the figure.

      Note:   You can select multiple blocks to connect them to a block that has multiple inputs, such as a bus.

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

Organize Your Model

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

  1. Drag a selection box around the Abs and Gain1 blocks.

  2. Move your mouse over the prompt that appears at the lower-right corner of the selection box. On the action bar, click Create Subsystem.

    A subsystem block appears in the model in place of the Abs and Gain1 blocks. To resize the subsystem block for the best fit in your model, use the block handles.

  3. Double-click the block name and type Absolute Value to give the subsystem a meaningful name.

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

      Tip   To use the Explorer Bar to navigate the model hierarchy, right-click the block and select Open in New Tab.

    The subsystem contains the Abs and Gain1 blocks that you selected as the basis of the subsystem. They are connected in sequence to two new blocks: In1 (an Inport block) and Out1 (an Outport block). Inport and Outport blocks correspond to the input and output ports on the subsystem.

  5. Double-click the name Out1 and type To Scope1.

  6. Click the Simulink Editor Up to Parent button to return to the top level of the model. The output port of the Absolute Value subsystem is now named To Scope1.

  7. You can name any signal. Double-click a signal and type the name. For example, double-click the signal from the Gain block and type To Scope. Be sure to double-click the signal and not a blank area of the canvas, which instead creates a separate text element (annotation). For other techniques that you can use with signal names, see Signal Name and Label Actions.

    The figure shows the model after you create the subsystem, name it, rename its output port, and name a signal.

    Tip   For additional resizing and alignment options, use the Diagram > Arrange menu.

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. In each scope, click the Autoscale button to view the entire signal.

    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 on an existing signal and 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.

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

  2. Drag the block and place it on 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 its position.

  3. Remove the Scope1 block. Press Shift and drag the Scope1 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.

    When you delete a block with 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 and place it at the end of the broken connection. The To Workspace block outputs the results to a variable in the MATLAB workspace.

  5. Rename the output port on the Absolute Value subsystem. Open the subsystem and rename the To Scope1 block as Workspace. Then, return to the top level of the model. The figure shows the current model.

  6. Simulate the model. The simout variable appears in the MATLAB workspace. You can double-click the variable to explore the results.

      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 Examples

Was this topic helpful?