Add package or class to current import list
import package_name.class_name1 package_name.class_name2...
import package_name1.* package_name2.*...
L = import
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.
This command may only be invoked at the command prompt. Attempting to use clear import within a function results in an error.
You can import packages and classes into a MATLAB workspace (from the command line or in a function definition). For example:
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:
For more information on how import works with MATLAB classes and packages, see Importing Classes.
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;
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.