フィルターのクリア

Change the position of a field in a structure

55 ビュー (過去 30 日間)
Abdulah Al Mubin
Abdulah Al Mubin 2021 年 3 月 3 日
コメント済み: Abdulah Al Mubin 2021 年 3 月 3 日
I want to add a new field to an already existed structure. But as I try to insert it, it goes at the end position. But I want to change its position to the top
Example:
x=
a: 1
b: 2
c: 3
......
now I insert
x.d=4
it shows
x=
a: 1
b: 2
c: 3
.......
d: 4
but I want it to show
x=
d: 4
a: 1
b: 2
c: 3
......
  5 件のコメント
Stephen23
Stephen23 2021 年 3 月 3 日
"I can’t write 'a','b','c' manually, I have more than hundred fields"
Did you try fieldnames, as I suggested?
Abdulah Al Mubin
Abdulah Al Mubin 2021 年 3 月 3 日
It worked, but I was hoping for a built-in function to reduce lines

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 3 月 3 日
編集済み: Walter Roberson 2021 年 3 月 3 日
Use struct2cell() on the old structure, and fieldnames() . Then insert a new cell of values for the new field at the top of the cell of data, and insert the new field name at the top of the list. Now use cell2struct() to create the new structure in the order you want.
This is the only way to control the order of fields when you insert a new field into a structure.
I would suggest to you, however, that it is easier to allow the new field to be put at the end of the struct fields, and then use orderfields() to change the field orders.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by