Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

import - Add package or class to current import list

Syntax

import package_name.*
import class_name
import cls_or_pkg_name1 cls_or_pkg_name2...
import
L = import

Description

import package_name.* adds specified package_name to the current import list. Note that package_name must be followed by .*.

import class_name adds a single class to the current import list. Note that class_name must be fully qualified (that is, it must include the package name).

import cls_or_pkg_name1 cls_or_pkg_name2... adds all named classes and packages to the current import list. Note that each class name must be fully qualified, and each package name must be followed by .*.

import with no input arguments displays the current import list, without adding to it.

L = import with no input arguments returns a cell array of strings containing the current import list, without adding to it.

The import function only affects the import list of the function within which it is used. When invoked at the command prompt, import uses the import list for the MATLAB command environment. If import is used in a script invoked from a function, it affects the import list of the function. If import is used in a script that is invoked from the command prompt, it affects the import list for the command environment.

The import list of a function is persistent across calls to that function and is only cleared when the function is cleared.

To clear the current import list, use the following command.

clear import

This command may only be invoked at the command prompt. Attempting to use clear import within a function results in an error.

Importing MATLAB Packages and Classes

You can import packages and classes into a MATLAB workspace (from the command line or in a function definition). For example:

import packagename.*

imports all classes and package functions so that you can reference those classes and functions by their simple names, without the package qualifier.

You can import just a single class from a package:

import packagename.ClassName
import Classname

You must still use the class name to call static methods:

ClassName.staticMethod()

For more information on how import works with MATLAB classes and packages, see Importing Classes.

Remarks

The import function allows your code to refer to an imported class by class name only, rather than with the fully qualified class name. import is particularly useful in streamlining calls to constructors, where most references to Java classes occur.

Examples

Add Java Class to Current Import List

import java.lang.String
s = String('hello');   % Create java.lang.String object

Add Multiple Java Packages to Current Import List

import java.util.* java.awt.*
f = Frame;             % Create java.awt.Frame object
methods Enumeration    % List java.util.Enumeration methods

See Also

clear, load, importdata

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS