What is the scope of the matlab import function?

I am trying to turn code written in Matlab into a standalone, compiled Matlab application. However, having received some odd errors, I realized that the code makes a lot of use of adding and removing from the path to circumvent the fact that several functions with the same name (but different results / calculations) are used several times. Looking around, I found that you can turn a folder into a package by placing a โ€œ+โ€ in front of your name and going through and making sure that the functions in this package access each other using name_of_folder.name_of_function. This solves the namespace problem, but it potentially creates a lot of work, since I now need to go through and add the correct package to each function call (and I still have to duplicate a lot of files).

Then I saw a function import, and I hope this saves me some time. I think I can pass the package that I want one or two specific functions, these functions import the package, and then everything will work the way I want - if the functions called by these functions fall into the scope of this import statement . For example, if I installed something like

function foo(var1, var2, ..., packagename)
  eval(sprintf('import %s.*', packagename));
  ...
  bar1(var1, var2);
  ...
  bar2(var2);
  ...

then I hope that bar1they bar2will use the package imported with the import statement. The documentation says that import instructions in conditional expressions and functions are limited to this code block, but I donโ€™t know if this code block means only this text or โ€œthis code blockโ€ is the code and everything that evaluates as a result. I have a feeling that this is the first, but I decided that I would ask in the hope that this was the last.

, ? , ?

+2
2

, , , import , , , , . :

function package_test(package_name)
  eval(sprintf('import %s.*;', package_name));

  test_function();
end

 

function test_function()
  nested_function()
end

function nested_function()
  disp('I\'m in the original folder :(');
end

, ,

function nested_function()
  disp('I\'m in the package! :)');
end

+trial. , package_test('trial'), " :(", , trial.nested_function() , .

, eval , import(sprintf('%s.*', package_name)); . , , .

+1

, , , " " . .

, ( ):

  • ( ) ( ) . , .

  • foo, bar car, , , bar car :

    function foo(parm1, parm2, parm3, version)
    if strcmp(version, 'bar')
        // bar code
    else
        // car code
    end
    

    , , , MATLAB ( , ). foo , , , .

+2

Source: https://habr.com/ru/post/1734329/


All Articles