Documentation Center

  • Trials
  • Product Updates

import

Add package or class to current import list

Syntax

import package_name.class_name
import package_name.function_name
import package_name.*
import package_name.class_name1 package_name.class_name2...
import package_name1.* package_name2.*...
L = import
import

Description

import package_name.class_name adds the fully qualified class name to the current import list.

import package_name.function_name adds the specified package-based function.

import package_name.* adds the specified package name. Note that package_name must be followed by .*.

import package_name.class_name1 package_name.class_name2... adds multiple fully qualified class names.

import package_name1.* package_name2.*... adds multiple package names.

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

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

The import function allows your code to refer to an imported class or function using fewer or no package prefixes.

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.

Limitations

  • import cannot load a Java® JAR package created by the MATLAB Builder™ JA product.

Examples

To add the containers.Map class to the current import list:

import containers.Map
myMap = Map('KeyType', 'char', 'ValueType', 'double');
 

To import two Java packages:

import java.util.Enumeration java.lang.String
s = String('hello'); % Create java.lang.String object
methods Enumeration % List java.util.Enumeration methods
 

To add the java.awt package:

import java.awt.*
f = Frame; % Create java.awt.Frame object
 

This example uses import in a function to call members of a .NET class in the System.Drawing namespace. Create the getPrinterInfo function:

function ptr = getPrinterInfo
import System.Drawing.Printing.*;
ptr = PrinterSettings;
end

To call the function, type:

NET.addAssembly('System.Drawing');
printer = getPrinterInfo;

More About

expand all

Tips

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.

If you use the import function in a control statement, for example, if or switch, or in a function, MATLAB limits the scope of the variables to that block of code. If you use the variables outside the function or control block, MATLAB displays an error message.

See Also

| |

Was this topic helpful?