Why does field order matter when defining structures?

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.

 채택된 답변

MathWorks Support Team
MathWorks Support Team 2009년 7월 10일

1 개 추천

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.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by