Hi all,
I am tip-toing into the Mata sea and have a question about working with a vector of structures that I hope to dynamically resize.
My .mata file is below. When I un-comment the failing line, here is the error I get:
Stata tech support confirms that the join operator (,) cannot deal with this, but that creating an instance of the struct, (x in my code below) allows the join. Their advice was to pre-allocate the vector to the length I will need. I can probably make that happen if there is no other way, but I'd rather build it dynamically.
So my real question is whether I am thinking about this wrong in some way?
Thanks in advance for any advice on this not-so-well formed question.
Cheers,
Nick Winter
Here is my .mata file:
I am tip-toing into the Mata sea and have a question about working with a vector of structures that I hope to dynamically resize.
My .mata file is below. When I un-comment the failing line, here is the error I get:
Code:
: myScale.scale_items = (myScale.scale_items,scale_item()) // does not work type mismatch: ,: struct scale_item found where transmorphic expected
So my real question is whether I am thinking about this wrong in some way?
Thanks in advance for any advice on this not-so-well formed question.
Cheers,
Nick Winter
Here is my .mata file:
Code:
clear * set matastrict on mata: struct scale_item { string vector stata_vars real vector num real vector denom real vector omit } struct scale { string scalar name struct scale_item vector scale_items // vector of items } myScale = scale() myScale.name = "This is my scale" myScale.scale_items=scale_item() // ititialize as 1 empty item length(myScale.scale_items) myScale.scale_items[1].stata_vars=("a1a") // now add second item to the scale // The following does not work: // myScale.scale_items = (myScale.scale_items,scale_item()) // but this does: x = scale_item() myScale.scale_items = (myScale.scale_items,x) length(myScale.scale_items) myScale.scale_items[2].stata_vars=("a1b") liststruct(myScale) end
Comment