Why can you import a package * after * using its contents in a function?

I am on MATLAB R2014b and ask a question, which I will illustrate in the following example.

MWE can be done as follows or download it as a .zip file here .

Create a package folder +test on your path with four function files in it:

 +test am bm cm dm 

am content:

 function a disp 'Hello World!' 

bm content:

 function b a 

If you run b from the command line, you will first need to import the test package ( import test.* ) Or run test.b

Running b will fail because scope b does not contain function a . We must import it before it can be used. For this, I created cm :

 function c import test.* a 

Now running c working fine.

Now my question. If I changed cm to (saved in dm ):

 function d a import test.* 

those. the import command is issued after calling the package function a . Running d still works fine, as if the position of the import command in dm doesn't matter. It seems that the import occurred before the function a called, which in dm happens in the line before the import.

Why is this happening. Is this the intended behavior and what is its use? How and in what order does MATLAB read the .m file and process it? And no longer on the topic, but in general: how to import packages processed in different languages ​​compared to MATLAB, does the order of the commands make sense?

My proactive conclusion based on comments. It is probably best to use the import function at or near the beginning of MATLAB code. This clearly shows that the imported content is available throughout the element (for example, a function). It also prevents the incorrect assumption that the contents are not yet available or belong to another thing with the same name before importing.

+5
import package matlab packages
source share
1 answer

MATLAB performs static code analysis before evaluating a function to determine the variables / functions used by that function. The evaluation of import statements is part of this static code analysis. This is by design, because if you import package and then use its functions, MATLAB should know this while analyzing static code. As a result, no matter where you place the import statement in your function, it will have the same effect as at the beginning of the function.

You can easily verify this by looking at the import output, which lists all imported packages.

+test/am

 function a(x) disp(import) import test.* end test.a() % test.* 

This is why the documentation says so as not to put the import statement in a conditional expression.

Do not use import in conditional statements inside a function. MATLAB preprocesses the import statement before evaluating the variables in the conditional statements.

 function a(x) disp(import) if x import test.* else import othertest.* end end test.a() % test.* % othertest.* 

The only way to avoid this behavior is to let the static code analyzer determine (no doubt) that the import statement will not execute. We can do this by obtaining a conditional expression simply by a boolean value.

 function a() disp(import) if true import test.* else import othertest.* end end test.a() % test.* 

As for imports compared to other languages, it really depends on the language. For example, in Python, you must put import before accessing the contents of the module. In my experience, this is a typical case, but I'm sure there are many exceptions. Each language will be different.

+3
source share

All Articles