Desidero aggiungere dati a una matrice di strutture senza che i campi delle strutture aggiunte abbiano necessariamente lo stesso ordine come i campi delle strutture originali. Per esempio:Esiste una variante delle strutture Matlab che non impone l'ordine dei campi?
% Works fine:
students.name = 'John';
students.age = 28;
student2.name = 'Steve';
student2.age = 23;
students(2) = student2;
% Error if the order of the fields of student2 is reversed
students.name = 'John';
students.age = 28;
student2.age = 23;
student2.name = 'Steve';
students(2) = student2; % Error: Dissimilar structs
C'è una variante di struct posso aggiungere dati senza dover mantenere lo stesso ordine dei campi?
MODIFICA: una soluzione consiste nell'utilizzare sempre i "campi dell'ordine" matlabs, che ordinano i campi in ordine alfabetico. Cioè, l'esempio errato di cui sopra diventerebbe:
% Order fields alphabetically
students.name = 'John';
students.age = 28;
student2.age = 23;
student2.name = 'Steve';
students = orderfields(students);
student2 = orderfields(student2);
students(2) = student2; % Works
Non sono sicuro se questa sia la soluzione più naturale.
probabilmente l'applicazione 'orderfields' ad entrambe le strutture corregge questo. – Daniel
Grazie, ho visto il tuo commento troppo tardi! – jolo