Main Content

import

Add package, class, or functions to current import list

Description

example

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

The import list scope is defined as follows:

  • Functions, including nested and local functions — The scope is the entirety of the function. This includes code that precedesimport.

    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. Do not callclear importwithin a function or a script.

    Scripts — The scope of a script is the entire script body. This includes code that precedesimport. The imports in a script are available only in the script body and are not available in the scopes which call the script. For example, executing a script containing imports at the command prompt does not make the imports available in the command window.

  • Base workspace — The scope is code executed at the command prompt. To clear the base import list, typeclear importat the MATLAB®command prompt.

example

importPackageName.FunctionName添加了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.ClassName.staticMethodName添加了specified static method. Use this syntax to shorten the name of a specific static method.

example

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

Avoid using this syntax, as importing the contents of 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.

example

importdisplays the current import list in the scope.

L= importreturns the current import list.

Examples

collapse all

importjava.util.Currencyjava.lang.String

Create ajava.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
方法类java.util.Currency: = 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 thesdpackage 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 simple namereadChunk, but only within the scope ofmyfunc.

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

Import themeta.class.fromNamestatic method in a function,myFunc. You can call the static method using the simple namefromName, but only within the scope ofmyFunc.

functionmetaClsObj = myFunc(ClassName) importmeta.class.fromNamemetaClsObj = fromName(ClassName);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 to use a shortened 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 string or character vector.

Example:matlab.io.hdf4

Name of the class, specified as a string or character vector.

Example:Currency

Name of the package function, specified as a string or character vector.

Example:readChunk

Name of the static method, specified as a string or character vector.

Example:fromName

Data Types:char|string

Output Arguments

collapse all

Import list, returned as a cell array of character vectors.

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.

Compatibility Considerations

expand all

Behavior changed in R2019b

Introduced before R2006a