Main Content

Importing Domain and Component Classes

You must store Simscape™ model files (domains and components) in namespace folders, as described in Organizing Your Simscape Files. Like the MATLAB® class system, each namespace defines a scope (or namespace). You can uniquely identify a model class name and access it using a fully qualified reference. For example, you can access the domain model class electrical using foundation.electrical.electrical.

In composite components, class member declarations include user-defined types, that is, component classes. If you do not use import statements, accessing component class names from a different scope always requires a fully qualified reference. For example, the Foundation library Resistor block is:

foundation.electrical.elements.resistor

An import mechanism provides a convenient means to accessing classes defined in different scopes, with the following benefits:

  • Allows access to model class names defined in other scopes without a fully qualified reference

  • Provides a simple and explicit view of dependencies on other namespaces

There are two types of syntax for the import statement. One is a qualified import, which imports a specific namespace or class:

import namespace_or_class;

The other one is an unqualified import, which imports all subnamespaces and classes under the specified namespace:

import namespace.*;

The namespace or class name must be a full path name starting from the library root (the top-level namespace folder name) and containing subnamespace names as necessary.

You must place import statements at the beginning of a Simscape file. The scope of imported names is the entire Simscape file, except the setup section. For example, if you use the following import statement:

import foundation.electrical.elements.*;

at the beginning of your component file, you can refer to the Foundation library Resistor block elsewhere in this component file directly by name:

rotorResistor = resistor(R = rotor_resistance);

See the import reference page for syntax specifics. For an example of using import statements in a custom component, see the Transmission Line example. To view the Simscape file, open the example, then double-click Open the transmission line component library. In the TransmissionLine_lib window, double-click the T-Section Transmission Line block and then, in the block dialog box, click Source code.

Related Topics