Sort struct fields in gui

I double-click a struct var in the workspace, and it opens a "variables" window with two columns: field, value.
When I click the field button, I expect it to be sorted alphabetically, but it doesn't.
If I use orderfields(), then the fields are sorted in the gui.

2 个评论

Is there documentation that indicates that you can sort by clicking that header?
Zohar
Zohar 2022-1-4
编辑:Zohar 2022-1-4
Not sure, but this is the general convetion in tables. If that's not what it does, then what does it do?
I think I just figured it out: it sorts the fields in the order that they were added to the struct. This is just weird.

请先登录,再进行评论。

 采纳的回答

Walter Roberson
Walter Roberson 2022-1-5

0 个投票

In MATLAB, fields are not internally sorted for struct. struct('A', 1, 'B', 2) is not compatible for assignment or concatenation with a struct with the same content but fields reversed.
struct in MATLAB is not an abstract mapping of field names to content: struct fields are ordered in MATLAB.
Ordered fields is what is used in C and Fortran. struct in computing is tied to memory layout: once a field is created it is always at the same offset in memory, so it can be compiled as indexing that does not need to change as new fields are added (unless field reordering is done.)
There are, in computing, more general unordered mappings between names where the offset could potentially change as more fields are added. MATLAB offers containers.map for example.

3 个评论

Zohar
Zohar 2022-1-5
编辑:Zohar 2022-1-5
This explains why showing a specific order is necessary: order matters.
But it doesn't explain why when I click the field title in the gui, it's not sorting alphabetically regardless of the underlying structure. I have a lot of fields, and looking for a field by name is natural. Clicking and nothing happens or the current order is reversed (how is that helpful?) feels like a bug. To clarify, I have no problem that the default order is the real order in the struct. But in the viewer, when I click the column title, it should be sorted.
I can't recall a tool (e.g. excel or an html table), where a different convension was used.
But I guess this is the best explanation that I get.
"But it doesn't explain why when I click the field title in the gui, it's not sorting alphabetically regardless of the underlying structure."
Because that has not been implemented. Why it has not been implemented would be a question to ask of Mathworks. There might be an underlying limitation, or perhaps it just has not been a priority.
The Variable Brower is also a variable editor. If you sort a struct by field name and then edit a field, should the result stored back have the new field order, or does MATLAB need to keep track of the original order?
Whichever, but don't just invert the current "seemingly-random" order. But good points.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Structures 的更多信息

产品

版本

R2019b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by