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
folder:
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 folder.
To deploy this component as a block in your model:
Open or create a model.
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
.Double-click the block to open the source file selector dialog box.
Click to open the browser. The browser opens in the current working folder and lists only the files with the
.ssc
or.sscp
extension. Select themy_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 Description pane.The source component selection is applied immediately, regardless of whether you have the Auto Apply check box selected or not. The block icon and dialog box get updated, based on the selected source component.
Tip
Instead of browsing, you can type
my_resistor
directly into the text field. In this case, however, the Description pane does not automatically get updated. If you want to view the block name, description, or source code, click . To view the block parameters, click the Settings tab.
Related Examples
- Model Linear Resistor in Simscape Language
- Customize Block Display
- Switch Between Different Source Components
- Prototype a Component and Get Instant Feedback