Dynamically naming struct instances
niallrobinson
STRUCT aStructIDefinedEarlier $string
STRUCTREF localname = $string
localname.attrib1 = 100
but with real Igor code! As far as I can tell this isn't possible, however it would be very useful.
Thanks for you help
Niall
More to the point, there is little to be gained from this since STRUCTs cannot be stored persistently or accessed from other functions or the commandline (they are limited to function scope).
Consider this:
This statement causes Igor to allocate some temporary storage that will exist for the duration of the current function. The 'localName' identifier is just a handle that allows you to access that storage, but it is not part of the data! In effect the name of the object is just an agreement between you and the compiler. And because STRUCT is scoped to the function, it will disappear when the function exits, and cannot be accessed from other functions. So a name-based lookup is impossible anyway.
In your original post, try mentally replacing 'STRUCT' with 'variable' or 'string' to make it easier to see why.
Moreover, if that syntax were legal, you'd just end up with STRUCT declarations immediately followed by STRUCTREF declarations, and you would gain nothing. To see that, you can consider the following:
STRUCTREF localname = $string
localname.attrib1 = 100
is indistinguishable from
localname.attrib1 = 100
I think that what you may be getting at, is that you would like to have the user dynamically add additional STRUCT instances at runtime. That would be similar to how waves can have a variable number of points (consider the relation between variables and waves - it's completely analogous).
The solution to this would be waves containing STRUCTs, possibly limited to free waves only. But I don't think that is possible with the current Igor. I would like to see it too, though.
If this is really what you want then I think your only solution, at present, is to divide your struct members over different waves, or possibly a single multidimensional wave, and then use Redimension, InsertPoints, etc. calls.
March 22, 2011 at 09:48 am - Permalink
But you can use StructPut and StructGet to move between strings and STRUCTs. The strings can be global, and can be named using $string. You can also store the string as userdata in a control or window. There are restrictions on the types you can use in STRUCT members when you store them using StructPut.
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
March 22, 2011 at 10:06 am - Permalink
As for context: I have a certain criteria that define different selections from a dataset. I would like to use a structure to store the various definitions (i.e. about six variables). I would also like the user of a GUI to be able to edit, destroy and create these definitions. I would wan't these instances to be persistent.
Basically I am trying to wangle a way of make my code a little more object oriented (I know Igor doesn't really support OOP). The advantages would just be keeping the code tidy and constraining what information is needed for each "definition object". If there is no better way then I can do it using a series waves to store the six or so numbers.
Thanks again
Niall
March 22, 2011 at 11:53 am - Permalink
March 22, 2011 at 11:59 am - Permalink
The definitions, meaning their non-struct representations, need to be persistent because you need Igor to remember them while no procedure is running (such as while the user is deciding which buttons to press).
That seems like a possibility. You'd use StructPut to save the data, and StructGet to convert it back into the structure. Between uses I think you'll need to use one or more waves to store these definitions. Following John's suggestion, you could use a text wave with StructPut/StructGet, or you could use one or more numeric waves.
March 22, 2011 at 12:57 pm - Permalink
Thanks
Niall
March 23, 2011 at 04:50 am - Permalink