Why create a 1-by-3 structure array with one field by this way?
Show older comments
Accepted Answer
More Answers (1)
Manan Jain
on 22 Jul 2023
0 votes
Hi!
Let's discuss the differences between the approaches:
- S.a = []; This approach creates a single field a in the structure S and assigns an empty array to it. The result is that all elements of S will share the same a field.
- S(2).a = []; This approach creates an array of structures S, and then assigns an empty array to the a field of the 2nd structure. The other structures still have an empty a field because it wasn't explicitly assigned for them.
- S(3).a = []; This approach is similar to the previous one, but it assigns an empty array to the a field of the 3rd structure.
So, the main difference between the two approaches is how they handle the field a in the structures:
- In the first approach (S.a = []), all structures in the array S share the same field a.
- In the second approach (S(2).a = []), only the 2nd structure has the field a.
- In the third approach (S(3).a = []), only the 3rd structure has the field a.
Which approach is better depends on your specific use case and requirements. Here is what you can look while deciding:
- If you need all structures to have the same field a with the same value, then the first approach (S.a = [];) is more appropriate.
- If you have specific elements in the array S that require the field a, then the second or third approach (S(2).a = []; or S(3).a = [];) can be used to assign the field only to those specific elements.
I hope this helps!
Thanks
6 Comments
"The result is that all elements of S will share the same a field."
Yes, but because S only has one element this statement is rather misleading as it implies multiple elements.
"If you need all structures to have the same field a with the same value, then the first approach (S.a = [];) is more appropriate."
Another misleading statement: this is only true if S is scalar (either scalar S exists or if S does not exist before the assignment, in which case it will be created scalar). Otherwise if S exists and is non-scalar then this code is an error:
S = struct('x',{1,2,3}) % non-scalar
S.x = []
So every mention of "all structures" or "all elements" with reference to the sytnax S.a = [] is strictly correct because all of one element is indeed one element, but highly misleading becaue it appears the author thinks that that syntax refers to multiple elements of S (which it does not and cannot, as the author themselves could easily have demonstrated if thay had tested their examples before posting on this forum).
fa wu
on 22 Jul 2023
"S.a=[] creat 1x1 empty structure fist in memory"
No, a 1x1 array is not empty, it is scalar. Any array that is 1x1 is scalar: that is the definition of scalar.
The number of fields a structure array (or what their content are) is totally independent from the size of a structure array: it is possible to have an empty structure with 1000 fields. Or a 1x1000 structure with no fields.
"S(2).a=[]; creat 1x2 new structure in memory,and then copy [] from old S.a and past [] to new S structure"
Considering that structures are basically references to those numeric arrays, I doubt that MATLAB needs to copy the numeric arrays. Why would it waste time doing that? It just needs to copy the reference.
fa wu
on 22 Jul 2023
Anotherway to allocate array of structure is
S = repmat(struct('a',[]), 1, 3)
% But this will return an unexpected result for some
S = repmat(struct('a',{}), 1, 3)
"teacher said "if you know the structure size is n,you should creat S(n).xx=[]. It will make your programe run fast,because it will Pre requested memory""
They are certainly right that a long time ago, this approach was blazingly fast for preallocating numeric arrays. Whether it still is today, I have not checked (but given the significant changes to the JIT engine etc. I would not presume this to be still true or significant, without further testing).
In any case, preallocating numeric arrays is much more robust with ZEROS, NAN, etc.
The code I gave you here also preallocates the entire structure array, with the added benefit that it is also much more robust as well. Perhaps your teacher did not really understand how to write robust and efficient code.
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!