struct function in Matlab
11 views (last 30 days)
Show older comments
Hi there,
I want to generate more than 10^4 times of a random 3 dimension matrix, is it a good and efficient way to store all those 10^4 3D matrices with 'struct' function? My understanding of 'struct' function is a group of variables with different types. Not sure if it is suitable to store numerous datas. And would it be easy and efficiency to access those datas? And how does it compare with a 4D matrix?
7 Comments
Dyuman Joshi
on 21 Aug 2023
Thank you for pointing out the mistake Bruno, I have edited my comment accordingly.
Answers (2)
Bruno Luong
on 21 Aug 2023
I recommand ro Store them in 4D array
16 x 16 x 16 x 1e4
No reason to use struct or cell or any other structure. It would make the processing more cumbursome.
0 Comments
Walter Roberson
on 22 Aug 2023
It looks to me as if the memory occupied by a struct is:
- for each field, 160 bytes plus 8 bytes times the number of struct elements
- for each extra field entry that has been written to, 96 bytes of overhead, plus the memory required for the data
So for example, foo(100).bar = []; requires 160 + 8 * 100 = 960 bytes. If you now assign to foo(99).baz = []; then that requires another 106 * 8 + 100 = 960 bytes for the second field. If you then assign to foo(42).bar = []; then that requires an additional 96 bytes plus the 0 bytes for the data.
This despite the fact that after you assign to foo(100).bar = [] that you create an implicit recall of foo(42).bar is [] because it is unassigned -- the explicit foo(42).bar = [] needs the 96 bytes overhead.
If you investigate cell arrays, you can deduce that 96-ish bytes (sometimes it seems like 100 or 108 bytes) is the number of bytes needed to store the dimensions and type and "iscomplex" and "isglobal" flag and other information about a variable -- the 96 bytes is the overhead for describing an anonymous variable.
And we notice that the 8 bytes (from "times the number of struct elements") is the exact same size as a pointer.
This suggests that each field of a struct is implemented as an array of pointers; if the pointer is empty then that offset of the field contains no explicit data and should read back as double precision []. If the pointer is not empty, it is the descriptor of an anonymous variable.
The size() of a struct does not appear to depend upon the length of the variable name for the field, suggesting that the field names are stored fixed-width. With 64 characters fixed width and 96 bytes of descriptor of some sort that would add up to 160 bytes, which does not sound like a coincidence.
... The point of all of this (besides being of interest for future reference) is that storing as a struct array can have significant overhead compared to just storing as a 4D array.
1 Comment
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!