I'm trying to turn some code written in Matlab into a standalone, compiled Matlab application. However, after getting some odd errors, I realized that the code makes a lot of use of adding and removing from the path to get around the fact that there are several functions with the same name (but different results/calculations) used multiple times. Looking around, I discovered that you can turn a folder into a package by putting a "+" in front of its name, and going through and making sure the functions in that package refer to each other using name_of_folder.name_of_function
. This solves the namespace problem, but it potentially creates a lot of work, in that I now have to go through and prepend the correct package to each function call (and I may still end up having to duplicate a lot of files).
Then I saw the import
function, and I'm hoping that'll save me some time. I think I can pass the package I want to one or two particular functions, have those function import the package, and then things will work the way I want--if the functions that those functions call fall into the scope of that import statement. E.g, if I set something up like
function foo(var1, var2, ..., packagename)
eval(sprintf('import %s.*', packagename));
...
bar1(var1, var2);
...
bar2(var2);
...
then I'm hoping bar1
and bar2
will use the package imported with the import statement. The documentation says that import statements in conditionals and functions are limited to that block of code, but I don't know if "that block of code" means that text only, or "that block of code" being the code and everything that evaluates as a result. I have a feeling it's the former, but I figured I'd ask in the hopes that it is the latter.
So, what is the scope of an import statement? Alternatively, is there another way to deal with this problem?