Dynamically naming struct instances

Hi - I was wondering if it is possible to dynamically name a struct instance. I have defined a struct and would like the user to be able to create instances of it through a GUI. Something like this:
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
I'm having a hard time seeing why that would be useful. Could you elaborate some more?

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:
STRUCT aStructIDefinedEarlier localName

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:
STRUCT aStructIDefinedEarlier $string
STRUCTREF localname = $string
localname.attrib1 = 100


is indistinguishable from
STRUCT aStructIDefinedEarlier localName
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.
Yep, STRUCTs in Igor are not global objects.

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
First of all - I've been an Igor programmer for 3 or 4 years and this is the first time I've used Igor exchange. Thanks for the very prompt responses.

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
Just thinking about what johnweeks said, I could "store" in info in waves but load them into structs when the GUI needs (using structput), treat them as structs, and then store all any user created data in more waves?
niallrobinson wrote:
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.

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).

niallrobinson wrote:
Just thinking about what johnweeks said, I could "store" in info in waves but load them into structs when the GUI needs (using structput), treat them as structs, and then store all any user created data in more waves?


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.
Just to say that I have now successfully used structget and structput in my program. I have a text wave of "struct names" that corresponds to the columns of a matrix wave, each of which holds an instance of the struct. Whenever I want to use one I just search for the correct name in the text wave and use that index to structget from the matrix.

Thanks
Niall