What is an import list?

Why would I want to add packages or classes to an import list?

Answers (1)

Sean de Wolski
Sean de Wolski on 14 Jul 2016
Edited: Sean de Wolski on 14 Jul 2016
So you can use the shortname
pack1.pack2.file(pi)
or
import pack1.pack2.*
file(pi)
I tend to do the import if I need more than one file in a package or need to reference one file multiple times.

4 Comments

So I guess you provided an example of using the import command? What is being imported? Where is it being imported to?
I have a toolbox where a program by the publisher of the toolbox uses import. Why is that needed for that toolbox, but when I use a Mathworks toolbox I don't have to do that? With Mathworks Toolboxes, I just call the function name with no import statement needed at all.
I don't think you ever need to import a package or Java name, but it can be convenient to do so.
As to why you don't need to import to use MathWorks functions, most MathWorks functions are not in a package. Many MathWorks function predate the introduction of packages, in fact. There are some exceptions; for instance, in the unit testing framework there are packages like matlab.unittest.constraints or matlab.unittest.fixtures.
While you could call matlab.unittest.constraint.IsEqualTo every time you needed to use it, that's a lot of typing. Using import matlab.unittest.constraint.IsEqualTo and then simply calling IsEqualTo will save typing if you need to call IsEqualTo multiple times. [Of course, this is probably not a great example; usually I would call the verifyEqual method on the test object instead of calling the constraint directly. But that was just an example, it doesn't need to necessarily be realistic.]
Another example is the mlreportgen.dom.* package. I will often import it so that I can use components all based on shortname (Text, Image, Document rather than mlreportgen.dom.Text, mlreportgen.dom.Image,...). This is a prime example of why things are in the package, image and text are already existing MATLAB functions but by putting them in the package the report generator ones can coexist with the base MATLAB ones.
As for your question Stephen, importing just means that you can use the shortname. That is all. It's not being imported anywhere, you can think of it as a flag if you want: useshortname.

Sign in to comment.

Categories

Find more on Software Development in Help Center and File Exchange

Asked:

on 14 Jul 2016

Commented:

on 19 Jul 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!