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].
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=0s, 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 variablei = 0 do duplicate/o/r=[i*4,(i+1)*4-1] w_s, w_0 structget/b=0r, w_0
// do something with r.res, i-th int32 element of stuct s printr.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.
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.
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.
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™.
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:
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.
... 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.
January 13, 2018 at 12:48 pm - Permalink
variable var[10]
END STRUCTURE
Function ...
STRUCT myWaveStructure mws
mws.var[4] = 4.56
...
end
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
January 13, 2018 at 01:17 pm - Permalink
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
andstructput
. 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
January 16, 2018 at 05:32 am - Permalink
January 16, 2018 at 06:26 am - Permalink
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
January 16, 2018 at 09:59 am - Permalink
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. Ifstructput
were to dump type annotated data, it would've been a thing of beauty™.Next time.
best,
_sk
January 17, 2018 at 12:55 am - Permalink
@thomas_braun: Cheers
best,
_sk
January 17, 2018 at 01:03 am - Permalink
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:
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.
January 17, 2018 at 09:57 am - Permalink
January 18, 2018 at 06:21 pm - Permalink
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.
January 19, 2018 at 06:01 am - Permalink
January 19, 2018 at 10:37 am - Permalink