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.
import package matlab packages
Erik
source share