Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • mata function available in different ado-files

    Dear all,

    I am trying to execute a mata function in different but related ado-files, but I don't know how to do it efficiently. Right now, I have the same mata functions at the end of each ado-file. Let me explain.

    I have an ado file called `xyz.ado` that executes several other subroutines depending on the needs of the user. For instance, it may execute routine `xyz_load.ado` or `xyz_create.ado` or `xyz_estimate.ado`. Each of these ado-files uses a mata function called `hello()`. My problem is that if I defined `hello()` as shown below, it is not accessible by any of the `xyz_SUBROUTINES.ado` adofiles,

    Code:
    program define xyz
            .
            xyz_load ...
            .
    end
    
    version 15
    mata:
    void hello() {
        printf("hello people\n")
    }
    end

    The restrictions that I face are the following:

    1. Each of the subroutnines `xyz_SUBROUTINES.ado` must remain as independent ado-files. That is, I cannot do the following,

    Code:
    program define xyz
            .
            .
            .
            xyz_load ...
            .
            .
            .
    end
    program define xyz_load
            .
            mata: hello()
            .
    end
    
    mata:
    void hello() {
        printf("hello people\n")
    }
    end
    2. the whole `xyz` package is used for several people in my team, so the mata function should available in all the subrountines each time the user executes `xyz`
    3. all the files of the package `xyz` won't be placed in the same directory path in the computer of each of the members of my team. That is, some people may have it in c:/ado/plus/x, others in c:/ado/personal/x, and others in c:/onedrive/stata/ (the ones with the latter directory path structure have already included such a paths in searching directories of stata using adopath ++)

    So, my questions are the following.

    1. Is it possible to compile a mata library from a .mata file directly from within an ado-file?
    2. When I placed the in `xyz.mata` with all my mata function in c:/ado/plus/x, and then execute `do xyz.mata` directly within my `xyz.ado` it does not read the version in c:/ado/plus/x but the one in my current directory. How do I make Stata to look for .mata file in the searching directories stored in the global macro S_ADO?


    Thank you so much for your help.

    Best,




    Best,
    Pablo Bonilla

  • #2
    Hi Pablo,

    I've thought about this issue a lot, and tried several solutions, from including compiled Mata files, to compiling on-the-go, etc.

    What I think is by far the best approach is to just have a mata file with all the code, and include it at the end of each ado. For instance, this is what I do in the ppmlhdfe package:

    https://github.com/sergiocorreia/ppm...lhdfe.ado#L370

    Code:
    findfile "ppmlhdfe.mata"
    include "`r(fn)'"
    By using "filefile", you will search all the possible folders for the package (see "adopath"), and by including, you actually get faster code than if you were to call a compiled Mata file.

    Comment


    • #3
      This is an excellent solution, Sergio. Thank you so much. I always appreciate your help.

      Best,
      Best,
      Pablo Bonilla

      Comment

      Working...
      X