fieldnames not order as expected

Hi:
I saved a serious of parameters in order into a matfile, but after I read it, the 'fieldnames' function does not return the field names neither in order of time saved nor in order of parameter names in alphabete.
This is not a big issue and I can fix it by adding some additional rows of command, but still ask here to want know if there is any reason behind this phenomenon. below are the test code:
para1=1:5;
para2=1:5;
para3=1:5;
save('test','para1','-v7.3');
save('test','para2','-append','-nocompression')
save('test','para3','-append','-nocompression')
tmp=matfile('test');
fieldnames(tmp)
Thanks!
Yu

6 件のコメント

Walter Roberson
Walter Roberson 2019 年 5 月 6 日
fieldnames does not apply to matfile objects. matfile makes the names available as property names not as field names.
Yu Li
Yu Li 2019 年 5 月 6 日
Thank you Walter, my question is that, since fieldnames does not apply to matfile, why I can use that without any error message?
Adam Danz
Adam Danz 2019 年 5 月 6 日
In case you ever wanted to list the filenames of a structure alphabetically, use orderfields()
Yu Li
Yu Li 2019 年 5 月 7 日
Thanks for your reply, but this function is for structure, not for matfile.
Bests,
Yu
Adam Danz
Adam Danz 2019 年 5 月 7 日
編集済み: Adam Danz 2019 年 5 月 8 日
Yes, that's what my comment indicates as well. It addressed your original inquiry,
"the 'fieldnames' function does not return the field names neither in order of time saved nor in order of parameter names in alphabete."
Walter Roberson
Walter Roberson 2019 年 5 月 7 日
sort(properties(tmp))
There was never any promise that properties (or fields) would be in a particular order when requested.
I chased through the code again, and I was able to prove that the properties are added in the order you would expect. However, the built-in addprop@dynamicprops https://www.mathworks.com/help/matlab/ref/dynamicprops-class.html does something that results in the properties not being reported back in the expected order.
The exact order that properties appear in is not consistent, just consistent in small runs. I speculate that it ends up doing something like sorting by internal address of the struct it creates.

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2019 年 5 月 6 日

編集済み:

2019 年 5 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by