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, named my_resistor, in your working directory:

component my_resistor
% Linear Resistor
% The 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 in Model Linear Resistor in Simscape Language. You can copy the source from this page and save it as my_resistor.ssc in 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 the Simscape Component block 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 is Unspecified.

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

  4. Click to open the browser. The browser opens in the current working directory and lists only the files with the .ssc or .sscp extension. Select the my_resistor.ssc file and click Open. 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 the preview pane.

    Tip

    Instead of browsing, you can type my_resistor directly into the text field. In this case, however, the preview pane does not automatically get updated. If you want to preview the block name, description, or source code, click .

  5. Click Apply. The block icon and dialog box get updated, based on the selected source component.

Related Examples

More About