Announcement

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

  • Create Functions using Mata

    Hi All,

    I am new to the Mata programming and currently trying to convert some codes from Matlab to Stata using Mata. I am sorry if my question seems rather straightforward.

    I would need to create functions that can be ran individually. Should I use ado files or do-files to create the functions?

    Also, when I tried to run a do-file with Mata, it seems that the program cannot be ran automatically by hitting the Execute button. Does that mean I shouldn't add the command "Mata" and "end" to the do-file?


    Thank you in advance!

  • #2
    It is a bit hard to answer your question, because you don't show what you have done. You can define a function in Mata in a .do file or in an .ado file. You could for example copy the code below in a .do file and you should be able to run it.

    Code:
    clear all
    mata:
    string scalar hello(string scalar who) {
        return("hello " + who)
    }
    
    hello("world")
    hello("Scarlet")
    end
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X