Search code examples
matlabnamespaceseditorprojects-and-solutions

Reference function within package matlab


I would like to structure my project in several packages. Each package should be each own namespace (so as to avoid conflicting filename) but within a package, I want everything to be in the same namespace (without having to put all the files in the same folder; I'd like different folders).

In practice I would like this structure

Project 
    main.m
    commonLibrary
    +part1Project
        mainPart1.m
        otherFolder
            supportFile.m
    +part2Project
        mainPart2.m
        otherFolder2
            supportFile2.m

This is the behavious I would like:

  • When in main.m, I can call everything in common library and everything in any sub-project, including the functions inside the subfolder. So I would like to call part1Project.supportFile
  • When in mainPart1.m, I want to call the support files without using the prefix of the current package (i.e. I want to call supportFile directly)
  • When in mainPart2, I want to call supportFile2 directly. If I want access to files in the the part 1 of the project, I can call part1Project.supportFile.

The current setup is that I added the Project folder and all the subfolders to the matlab path. But this means that

  • I CANNOT call supportFile from anywhere; not from main (part1Project.supportFile will not work) and not even from mainPart1 (supportFile can't be found)
  • Much in the same way, it is hard to access elements of part1Project from part2Project

How can I achieve the behaviour I want?


Solution

  • You cannot access functions within a subfolder of a package unless that subfolder is a private folder in which case it will only be accessible to the functions in the immediate parent folder.

    If you do use the private folder approach, then you can call functions within this private folder from the functions in the containing folder without using the fully-qualified package name.

    Your layout would look like:

    Project 
        main.m
        commonLibrary
        +part1Project
            mainPart1.m
            private
                supportFile.m
        +part2Project
            mainPart2.m
            private
                supportFile2.m
    

    Your first point will not work but the other two will. There is no built-in way to accomplish the first point.

    Another option would be to use import statements in all functions within each package such that it imports all package members at the beginning of the function.

    Your layout would look like

    Project 
        main.m
        commonLibrary
        +part1Project
            mainPart1.m
            supportFile.m
        +part2Project
            mainPart2.m
            supportFile2.m
    

    And the contents of mainPart1.m (any any function) would look something like:

    function mainPart1()
        % Import the entire namespace
        import part1Project.*
    
        % No package name required
        supportFile()
    end
    

    And then from main you could access supportFile

    function main() 
        part1Project.supportFile()
    end