MATLAB Answers

2

Sorting variable names in alphabetical order

Asked by Jonny Coltraine on 20 Nov 2012
Latest activity Commented on by Walter Roberson
on 6 Jan 2016
Using the workspace window, wow can I sort variables defined inside a struct object in alphabetical order? clicking on the Field column doesn't sort them.

  0 Comments

Sign in to comment.

2 Answers

Answer by José-Luis
on 20 Nov 2012
Edited by José-Luis
on 20 Nov 2012
 Accepted Answer

Looks like you might want to use orderfields()
a.b = 1;
a.a = 2;
a.m = 4;
a.h = 5
a =
b: 1
a: 2
m: 4
h: 5
a = orderfields(a)
a =
a: 2
b: 1
h: 5
m: 4

  0 Comments

Sign in to comment.


Answer by Daniel Shub
on 20 Nov 2012

Assuming you mean the variable editor and not the workspace window, I think this is a possible bug. There seems to me no good reason to have the ability to sort by field, and not have it perform an alphabetical sort.

  2 Comments

I don't think it is a bug... same issue here with MATLAB 2015a. Clicking on "Field" in the Variables window sorts the fields, but it appears to be a random sort; it is not alphabetical.
MATLAB structs are field order dependent.
A = struct('field1', [], 'field2', []);
B = struct('field2', [], 'field1', []);
A(2) = B;
Subscripted assignment between dissimilar structures.
However, [A;B] is fine

Sign in to comment.