Why does field order matter when defining structures?

9 views (last 30 days)
Two structures with the same field names in a different order can not be set to equal each other, even though they are nearly identical.
This example shows the problem:
a(1).begin = 1;
a(1).end = 2;
b.end = 3;
b.begin = 4;
a(2) = b;
% Error :
??? Subscripted assignment between dissimilar structures.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Jul 2009
This is actually part of the design of structures in MATLAB. For various reasons, including performance, field order is significant when dealing with structures. Field order is significant in many other programming languages as well.
So when you are making structures, you should be aware of the field creation order. You may even want to consider using a function to initialize your structure before filling in the fields.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!