Structure by index

I just had a quick question about structures. Is there a way to reference a structure item using a numerical index? For example, if I want the 5th item in a structure called 's', I'd like to reference it using s[4].

Thanks,
Ben

You can create a structure only with a field defined as an array or with a wave. Is this not what you want?

STRUCTURE myWaveStructure
     variable var[10]
END STRUCTURE

Function ...

     STRUCT myWaveStructure mws
     mws.var[4] = 4.56
     ...
end


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
If you know the elements size you can do something like:
structure thunk_s
    int32 a
    int32 b
endstructure

structure thunk_res
    int32 res //same type as a and b
endstructure

function thunk_struct()
    struct thunk_s s
    struct thunk_res r
    s.a = -5
    s.b = 5
   
    make/o/n=1 w_s
    structput/b=0 s, w_s // w_s stores now struct s as a wave in an understandable but not a standard binary form
   
    variable v_sz = dimsize(w_s,0)/4 //4 is size of the int32 in the wave w_s
    variable i = 0
    do
        duplicate/o/r=[i*4,(i+1)*4-1] w_s, w_0
        structget/b=0 r, w_0
       
        // do something with r.res, i-th int32 element of stuct s
        print r.res
       
        i+=1
    while (i != v_sz)  
end


The key functions are structget and structput. Of course, knowing what is the structure has influenced me in the implementation of the code, maybe you can abstract it away. I don't know how.

best,
_sk
thomas_braun wrote:
@_sk: Nice hack!

I tried, and can't let this pass.

Ew. Can't say I agree, and I almost always agree with Thomas! This has all the bad elements of FORTRAN Common blocks, or C unions. I would only use this if there is no other way.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
johnweeks wrote:
thomas_braun wrote:
@_sk: Nice hack!

I tried, and can't let this pass.

Ew. Can't say I agree, and I almost always agree with Thomas! This has all the bad elements of FORTRAN Common blocks, or C unions. I would only use this if there is no other way.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com


Yeah, it ain't pretty, you can say that again. The implementation is ugly due to structput and the way it dumps the struct in the wave. If structput were to dump type annotated data, it would've been a thing of beauty™.

Next time.

best,
_sk
thomas_braun wrote:
@_sk: Nice hack!


@thomas_braun: Cheers

best,
_sk
There is a problem with using StructPut and StructGet - they do not work with reference variables such as WAVE, NVAR, SVAR and DFREF. The reason is that these are effectively pointers and the object pointed to may not exist when StructGet needs to restore them.

They also do not work with Strings because they are implemented internally using pointers to variable-length data.

It is possible to define a structure containing an array of structures and index into that. Here is an example:

Structure SubStruct
    Variable var
EndStructure

Structure MainStruct
    STRUCT SubStruct sub[100]
EndStructure

Function Demo()
    STRUCT MainStruct ms
   
    Variable i
   
    for(i=0;i<10; i+=1)
        ms.sub[i].var = i
    endfor

    for(i=0;i<10; i+=1)
        Print i, ms.sub[i].var
    endfor
End


A limitation is that an array of STRUCTs is limited to 100 elements.
thanks for the input everyone. I think @hrodstein wins for the easiest to implement solution. I came across this problem by trying to transcribe some matlab code into Igor. On a similar topic, the ability to list out the elements of a structure, their data types, size, etc. could be pretty useful - maybe a StructInfo command similar to TraceInfo.
bmb wrote:
... On a similar topic, the ability to list out the elements of a structure, their data types, size, etc. could be pretty useful - maybe a StructInfo command similar to TraceInfo.


I agree. This would, I think, allow one to create generic functions to store and retrieve all elements of a structure to and from a data folder without knowing beforehand the details of the structure.