Main Content

import

Import model classes

Syntax

import package_or_class;    
import package.*;

Description

The import statements allow access to model class or function names defined in other scopes (packages) without a fully qualified reference. You must place import statements at the beginning of a Simscape™ file.

There are two types of import statement syntaxes. One is a qualified import, which imports a specific package, class, or function:

import package_or_class;

The other one is an unqualified import, which imports all subpackages, classes, and functions under the specified package:

import package.*;

The package or class name must be a full path name, starting from the library root (the top-level package directory name) and containing subpackage names as necessary.

Import statements are subject to the following constraints:

  • The imported name must be a full path name, starting from the library root, even if the import statement is used in a component class defined under the same package as the domain or component class that is being imported.

  • 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.

  • In qualified import statements, the imported name can refer to a subpackage, a model class (domain class or component class), or a function. For example, in the import A.B.C; statement, C can be either a subpackage name, a class name, or a function name. In unqualified import statements, the imported name must refer to a package or subpackage. For example, in the import A.B.*; statement, B must be a subpackage name (of package A).

  • It causes a compilation error if an unqualified imported name is identical to other names within the same scope, provided the duplicate name is in use. For example, assume that subpackages A.B and A.B1 both contain a component class C. The following code:

    import A.B.C;
    import A.B1.*;
    component M
       components (ExternalAccess=observe)
         c = C;
       end
    end
    

    causes a compile-time error. However, the following code is legal (provided that D is defined only in A.B) because C is not used:

    import A.B.C;
    import A.B1.*;
    component M
       components (ExternalAccess=observe)
         d = D;
       end
    end
    

    This code is also legal:

    import A.B;
    import A.B1;
    component M
       components
         c1 = B.C;
         c2 = B1.C;
       end
     end
    

    because you import two different names into this scope (B and B1), and access the two different component classes C through their parent classes B and B1.

Examples

In this example, the composite component consists of three identical resistors connected in parallel:

import foundation.electrical.electrical;  % electrical domain class definition
import foundation.electrical.elements.*;  % all electrical elements
component ParElResistors
  nodes
     p = electrical;
     n = electrical;
  end
  parameters
    p1 = {3 , 'Ohm'};
  end
  components(ExternalAccess=observe)
    r1 = resistor(R=p1);
    r2 = resistor(R=p1);
    r3 = resistor(R=p1);
  end
  connections
    connect(r1.p, r2.p, r3.p, p);
    connect(r1.n, r2.n, r3.n, n);
  end
end

Version History

Introduced in R2013b