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

  • Referring to structure members in a function

    I am trying to write a function in which I give two arguments: one is a previously defined structure which basically contains the variables of my dataset, as column numerical or string vector, and the other would be the name of one of the variables (as a string).

    I have not managed to figure out yet how to use the name of the variable to access the member of the structure.

    Suppose the structure is called st1 and the string is string1 = "num".

    How do I access st1.num in the function by using string1?

  • #2
    I assume that "basically contains the variables of my dataset" means that the column numerical or string vector contains the dataset's variable index or variable name. If that's the case, then why are you storing that information in a struct when you can easily get that information on-the-fly anytime you want from built-in Mata functions?

    Are you trying to, for example, store in-sequence the names of variables in a dataset that has since been modified (new variables inserted)? Even then, you're better off using a dictionary (associative array) than in a struct.