Main Content

Deploy a Component File in Block Diagram

This example shows how you can quickly transform a Simscape™ component file into a block in your model, without the extra steps of packaging the file and building a custom library.

Suppose you have the following Simscape file, namedmy_resistor, in your working directory:

线性电阻组件my_resistor % %voltage-current (V-I) relationship for a linear resistor is V=I*R, % where R is the constant resistance in ohms. % % The positive and negative terminals of the resistor are denoted by the % + and - signs respectively. nodes p = foundation.electrical.electrical; % +:left n = foundation.electrical.electrical; % -:right end variables i = { 0, 'A' }; % Current v = { 0, 'V' }; % Voltage end parameters R = { 1, 'Ohm' }; % Resistance end branches i : p.i -> n.i; end equations assert(R>0) v == p.v - n.v; v == i*R; end end

Tip

This component implements a linear resistor. It is described in more detail inModel Linear Resistor in Simscape Language. You can copy the source from this page and save it asmy_resistor.sscin your working directory.

To deploy this component as a block in your model:

  1. Open or create a model.

  2. Open the Simscape > Utilities library and add theSimscape Componentblock to your model. At first, the block does not point to any component file. Therefore, it does not have any ports, and the block icon states it isUnspecified.

  3. Double-click the block to open the source file selector dialog box.

  4. Clickto open the browser. The browser opens in the current working directory and lists only the files with the.sscor.sscpextension. Select themy_resistor.sscfile and clickOpen. The name of the source file appears in the text field of the source file selector dialog box, and the block name, description, and the link to source code appear in theDescriptionpane.

    The source component selection is applied immediately, regardless of whether you have theAuto Applycheck box selected or not. The block icon and dialog box get updated, based on the selected source component.

    Tip

    Instead of browsing, you can typemy_resistordirectly into the text field. In this case, however, theDescriptionpane does not automatically get updated. If you want to view the block name, description, or source code, click. To view the block parameters, click theSettingstab.

Related Examples

More About