MATLAB Answers

How to save fields of a struct through for loop iteration.

14 ビュー (過去 30 日間)
Habtamu Tesfaw
Habtamu Tesfaw 2020 年 5 月 16 日
回答済み: 埃博拉酱 2021 年 7 月 23 日
I have a struct with several field names.
data=
struct with fields:
'a': [100×1 double]
'b': [400×1 double]
.
.
etc.
I can save all the filed values to the same file as follow.
save(filename, 'a', 'b', ...)
but, since the field name are too many, I want to to save all of them by for loop. Is there any way I can do that? Thank you!

採用された回答

Stephen
Stephen 2020 年 5 月 16 日
編集済み: Stephen 2020 年 5 月 16 日
You don't need a loop, just use the -struct flag to save the fields of a scalar structure:
save(filename,'-struct','data')
When you load the file, make sure that you use an output argument:
data = load(filename);
  1 件のコメント
Habtamu Tesfaw
Habtamu Tesfaw 2020 年 5 月 18 日
Thank you very much. Yes, it works great!

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

その他の回答 (1 件)

埃博拉酱
埃博拉酱 2021 年 7 月 23 日
Try this
save(filename,'-fromStruct',data);

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by