Add package, class, or functions to current import list
The import list scope is defined as follows:
Function or script, 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 the
import function for each function.
The scope of a script is the script body. 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.
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 the
clear function. Do not call
clear import within a function or a script.
Base workspace — Scope is code executed at the command prompt. To clear the base import list, type
clear import at the MATLAB command prompt.
import adds the specified package name.
PackageName must 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, the
matlab.io.hdf4.sd package has a
close function, which can conflict with the MATLAB
import java.util.Currency java.lang.String
java.lang.String object. There is no need to type the package name,
s = String('hello')
s = hello
Currency class methods, without typing the package name.
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 to
matlab.io.hdf4.sd package functions and avoid conflicts with the MATLAB
Display the full path to the example file
sd.hdf on your system using the shortened package name
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 the
sd package name.
There is no name conflict with the MATLAB
close function when you import the partial package name.
If you use the
matlab.io.hdf4.sd.* syntax to import the entire package name, when you call
close, MATLAB always chooses the package function. You cannot use
close to remove a figure.
matlab.io.hdf4.sd package function,
readChunk in a function,
myfunc. You can call the function using the simple name
readChunk, but only within the scope of
function data = myfunc(ID,n,m) import matlab.io.hdf4.sd.readChunk data = readChunk(ID,[n m]); end
meta.class.fromName static method in a function,
myFunc. You can call the static method using the simple name
fromName, but only within the scope of
function metaClsObj = myFunc(ClassName) import meta.class.fromName metaClsObj = fromName(ClassName); end
sd.hdf example file and access the
temperature data set.
import matlab.io.hdf4.* sdID = sd.start('sd.hdf'); idx = sd.nameToIndex(sdID,'temperature'); sdsID = sd.select(sdID,idx);
myfunc function from the previous example to
read the data.
myfunc must have its own
import statement to use a shorted package
dataChunk = myfunc(sdsID,0,1);
Close the file.
ans = 'java.util.Currency' 'java.lang.String' 'matlab.io.hdf4.*' 'matlab.io.hdf4.sd.readChunk'
PackageName— Name of package
Name of the package, specified as a string or character vector.
ClassName— Name of class
Name of the class, specified as a string or character vector.
FunctionName— Name of package function
Name of the package function, specified as a string or character vector.
staticMethodName— Name of static method
Name of the static method, specified as a string or character vector.
L— Import list
Import list, returned as a cell array of character vectors.
import cannot load a Java® JAR package created by the MATLAB
Compiler SDK™ product.
Do not use
import in conditional statements inside a function. MATLAB preprocesses the
import statement before evaluating the variables in the conditional statements.
Behavior changed in R2019b
Starting in R2019b, MATLAB changes the rules for name resolution, impacting the precedence order of variables, nested functions, local functions, and external functions. These rule are described in Function Precedence Order. For information about the changes and tips for updating your code, see Update Code for R2019b Changes to Function Precedence Order.
Identifiers cannot be used for two purposes inside a function
Identifiers without explicit declarations might not be treated as variables
Variables cannot be implicitly shared between parent and nested functions
Change in precedence of compound name resolution
Anonymous functions can include resolved and unresolved identifiers
The behavior of the
import function has changed.
Change in precedence of wildcard-based imports
Fully qualified import functions cannot have the same name as nested functions
Fully qualified imports shadow outer scope definitions of the same name
Error handling when import not found
Nested functions inherit import statements from parent functions