Announcement

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

  • how to read and write "struct matrix" elements in mata

    Dear all,
    im trying to figure out how to write and read elements from a "struct" matrix in stata

    Say that i have the following:
    Code:
    mata:
    
    mata
    struct coord  {
        real scalar x
        real scalar y
    }
    
    origen = coord()
    destination = coord()
    
    origen.x=1
    origen.y=1
    
    destination.x=3
    destination.y=3
    
    origen_destination = coord(2)
    
    end
    how do I write on the elements within "origen_destination"
    The following options do not seem to work

    Code:
    origen_destination[1,1]=origen
    origen_destination[1,2]=destination
    Thank you

  • #2
    So not sure if its the most elegant solution, but
    Code:
    mata:
    mata clear
    struct coord  {
        real scalar x
        real scalar y
    }
    
    origen = coord()
    destination = coord()
    
    origen.x=1
    origen.y=1
    
    destination.x=3
    destination.y=3
    
    origen_destination = coord(2)
    
    void write_info(struct coord matrix object1,struct coord inp, real scalar k){
        object1[k]=inp
    }
    
    real matrix read_info(struct   coord matrix object1, real scalar k){
        return((object1[k].x,object1[k].y))
    }
    
    write_info(origen_destination, origen,1)
    write_info(origen_destination, destination,2)
    
    read_info(origen_destination, 1)
    read_info(origen_destination, 2)
    
    end
    Seems that with struct, one needs functions that would "impute" or "export" the information

    Comment


    • #3
      Originally posted by FernandoRios View Post
      im trying to figure out how to write and read elements from a "struct" matrix in stata

      . . . how do I write on the elements within "origen_destination"
      The following options do not seem to work
      From what I've encountered, structs behave kind of like pointers, and so you can use a struct scalar to read and write data from and to an element of a struct array.

      You don't really need a function in order to "impute" or "export" information.

      Run the following code to see what I mean.
      Code:
      version 17.0
      
      clear *
      
      mata:
      
      struct Point {
          real scalar x, y
      }
      
      // First, a struct scalar
      scalar_point = Point()
      eltype(scalar_point)
      scalar_point
      scalar_point = NULL
      
      // Next, a struct vector
      Points = Point(2)
      
      // Empty at declaration
      liststruct(Points[1])
      liststruct(Points[2])
      
      // -point- is a scalar struct
      point = Points[1]
      orgtype(point); eltype(point)
      
      // But it behaves like a pointer to the first element of the struct vector:
      point.x = 0
      point.y = 1
      liststruct(Points[1])
      
      point.x = 2
      liststruct(Points[1])
      
      Points[1]
      point
      
      point = NULL
      
      // One retrieves the information analogously
      reader = Points[1]
      reader.x; reader.y
      
      end
      
      exit

      Comment


      • #4
        Ohh thank you
        this is what I needed

        Comment

        Working...
        X