import - Add namespace, class, or functions to current import list - MATLAB (original) (raw)
Add namespace, class, or functions to current import list
Syntax
Description
import [Namespace](#buvf42c-1-PackageName).[ClassName](#buvf42c-1-ClassName)
adds the class name to the current import list. To refer to a class without specifying the entire namespace, use the import
function 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 precedes
import
.
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 theclear
function. Do not callclear import
within 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, type
clear import
at the MATLAB® command prompt.
import [Namespace](#buvf42c-1-PackageName).[FunctionName](#buvf42c-1-FunctionName)
adds the specified namespace-based function. Use this syntax to shorten the name of a specific function in a namespace without importing every function in the namespace, which might cause unexpected name conflicts.
import [Namespace](#buvf42c-1-PackageName).[ClassName](#buvf42c-1-ClassName).[staticMethodName](#mw%5Fdf29d981-9f07-41a6-9843-6cab0ee7c7ac)
adds the specified static method. Use this syntax to shorten the name of a specific static method.
import [Namespace](#buvf42c-1-PackageName).*
adds the contents of the specified namespace identifier.Namespace
must be followed by.*
.
Avoid using this syntax, as importing the contents of namespaces 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 namespace. Then when you call a function, you use a shorter namespace name which does not conflict with simple function names.
import
displays the current import list in the scope.
[L](#buvf42c-1-L) = import
returns the current import list.
Examples
import java.util.Currency java.lang.String
Create a java.lang.String
object. There is no need to type the namespace identifier, java.lang
.
List the Currency
class methods, without typing the namespace identifier.
Methods for class java.util.Currency:
equals getAvailableCurrencies getClass getCurrencyCode getDefaultFractionDigits getDisplayName getInstance getNumericCode getSymbol hashCode notify notifyAll toString wait
Use partial namespace identifiers on your import list to simplify calls tomatlab.io.hdf4.sd
namespace functions and avoid conflicts with the MATLABclose
function.
Display the full path to the example file sd.hdf
on your system using the shortened namespace identifierssd
.
sdID = sd.start('sd.hdf'); filename = sd.getFilename(sdID)
filename =
_matlabroot_\matlab\toolbox\matlab\matlab_sci\hdf4\sd.hdf
Call the close function with the sd
namespace identifiers.
There is no name conflict with the MATLABclose
function when you import the partial namespace identifiers.
_matlabroot_\matlab\toolbox\matlab\graphics\close.p
If you use the matlab.io.hdf4.sd.*
syntax to import the entire namespace identifiers, when you call close
, MATLAB always chooses the namespace function. You cannot useclose
to remove a figure.
Import the matlab.io.hdf4.sd
Namespace function,readChunk
in a function, myfunc
. You can call the function using the simple namereadChunk
, but only within the scope ofmyfunc
.
function data = myfunc(ID,n,m) import matlab.io.hdf4.sd.readChunk data = readChunk(ID,[n m]); end
Import the matlab.metadata.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 myFunc
.
function metaClsObj = myFunc(ClassName) import matlab.metadata.Class.fromName metaClsObj = fromName(ClassName); end
Open the 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);
Call the myfunc
function from the previous example to read the data. myfunc
must have its ownimport
statement to use a shortened namespace identifier.
dataChunk = myfunc(sdsID,0,1);
Close the file.
sd.endAccess(sdsID) sd.close(sdID)
ans =
'java.util.Currency'
'java.lang.String'
'matlab.io.hdf4.*'
'matlab.io.hdf4.sd.readChunk'
Input Arguments
Namespace identifier, 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 namespace 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
Import list, returned as a cell array of character vectors.
Limitations
import
cannot load a Java® JAR namespace created by the MATLAB Compiler SDK™ product.- Do not use
import
in conditional statements inside a function. MATLAB preprocesses theimport
statement before evaluating the variables in the conditional statements.
Tips
- MATLAB preprocesses the
import
statement before evaluating other statements in the same file.
Version History
Introduced before R2006a
Previously, import would accept malformed arguments, such as #.b
or a.*.*
, and MATLAB would still execute even though no names would be imported. It would also allow importing names such as a.for
or a.if
that do not satisfy MATLAB naming rules. In R2023b, MATLAB issues a warning if you include invalid imports. These will become an error in a future release.
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