Documentation

import

Add package or class to current import list

Syntax

import PackageName.ClassName
import PackageName.FunctionName
import PackageName.*
import
L = import

Description

example

importPackageName.ClassNameadds the class name to the current import list. Use theimportfunction in your code to refer to a class without specifying the entire package name.

The import list scope is defined as follows:

  • Script invoked from the MATLAB®command prompt — Scope is the base MATLAB workspace.

  • Function, including nested and local function — Scope is the function and the function does not share the import list of the parent function. If the import list is needed in a MATLAB function or script and in any local functions, you must call theimportfunction for each function.

    The import list of a function is persistent across calls to that function and is cleared only when the function is cleared. For more information, see theclearfunction.

To clear the current import list, typeclear importat the MATLAB command prompt. Do not callclear importwithin a function or a script.

example

importPackageName.FunctionNameadds the specified package-based function. Use this syntax to shorten the name of a specific function in a package without importing every function in the package, which might cause unexpected name conflicts.

example

importPackageName.*adds the specified package name.PackageNamemust be followed by.*.

Avoid using this syntax, as importing packages brings an unspecified set of names into the local scope, which might conflict with names in the MATLAB workspace. One possible use for this syntax is to import a partial package name. Then when you call a function, you use a shorter package name which does not conflict with simple function names. For example, thematlab.io.hdf4.sdpackage has aclosefunction, which can conflict with the MATLABclosefunction.

example

importdisplays the current import list in the scope.

L= importreturns the current import list.

Examples

collapse all

importjava.util.Currencyjava.lang.String

创建一个java.lang.Stringobject. There is no need to type the package name,java.lang.

s = String('hello')
s = hello

List theCurrencyclass methods, without typing the package name.

methodsCurrency
Methods for class Currency: equals getDisplayName notify getAvailableCurrencies getInstance notifyAll getClass getNumericCode toString getCurrencyCode getSymbol wait getDefaultFractionDigits hashCode

Use partial package names on your import list to simplify calls tomatlab.io.hdf4.sdpackage functions and avoid conflicts with the MATLABclosefunction.

importmatlab.io.hdf4.*

Display the full path to the example filesd.hdfon your system using the shortened package namesd.

sdID = sd.start('sd.hdf'); filename = sd.getFilename(sdID)
filename = C:\Program Files\MATLAB\R2015a\toolbox\matlab\imagesci\sd.hdf

Call the close function with thesd包name.

sd.close(sdID)

There is no name conflict with the MATLABclosefunction when you import the partial package name.

whichclose
C:\Program Files\MATLAB\R2015a\toolbox\matlab\graphics\close.p

If you use thematlab.io.hdf4.sd.*syntax to import the entire package name, when you callclose, MATLAB always chooses the package function. You cannot usecloseto remove a figure.

Import thematlab.io.hdf4.sdpackage function,readChunkin a function,myfunc. You can call the function using the simplereadChunkname, but only within the scope ofmyfunc.

functiondata = myfunc(ID,n,m) importmatlab.io.hdf4.sd.readChunkdata = readChunk(ID,[n m]);end

Open thesd.hdfexample file and access thetemperaturedata set.

importmatlab.io.hdf4.*sdID = sd.start('sd.hdf'); idx = sd.nameToIndex(sdID,'temperature'); sdsID = sd.select(sdID,idx);

Call themyfuncfunction from the previous example to read the data.myfuncmust have its ownimportstatement in order to use a shorted package name.

dataChunk = myfunc(sdsID,0,1);

Close the file.

sd.endAccess(sdsID) sd.close(sdID)
import
ans = 'java.util.Currency' 'java.lang.String' 'matlab.io.hdf4.*' 'matlab.io.hdf4.sd.readChunk'

Input Arguments

collapse all

Name of the package, specified as a character array.

Example:matlab.io.hdf4

Name of the class, specified as a character array.

Example:Currency

Name of the package function, specified as a character array.

Example:readChunk

Output Arguments

collapse all

Import list returned as a cell array of character arrays.

Limitations

  • importcannot load a Java®JAR package created by theMATLAB Compiler SDK™product.

  • Do not useimportin conditional statements inside a function. MATLAB preprocesses theimportstatement before evaluating the variables in the conditional statements.

Introduced before R2006a

Was this topic helpful?