Assign a 'double' variable to struct

6 ビュー (過去 30 日間)
Ziv Kassner
Ziv Kassner 2018 年 1 月 10 日
コメント済み: Ziv Kassner 2018 年 1 月 11 日
I have a variable as such:
A.b = 'example';
And I want to change it to:
A.b.c = 2;
How can I do it without it prompting:
'Field assignment to a non-structure array object'
It worked on matlab's version before 2014.
Thank you,
Ziv
  2 件のコメント
Stephen23
Stephen23 2018 年 1 月 10 日
You can't.
You first define the field b as a double. You cannot then try to access field b as a structure.
Steven Lord
Steven Lord 2018 年 1 月 10 日
FYI this changed in release R2015b according to the Release Notes.

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

採用された回答

Jan
Jan 2018 年 1 月 10 日
編集済み: Jan 2018 年 1 月 10 日
A.b = 'example';
A.b = struct('c', 2); % Overwrite field "b"
Or:
A.b = 'example';
A.b = [];
A.b.c = 2
It is strange, that this works with the empty matrix, if it fails with an error for a char vector. But the first method is better, because it overwrites the field clearly.
  1 件のコメント
Ziv Kassner
Ziv Kassner 2018 年 1 月 11 日
Great! The 1st method made it happen!

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

その他の回答 (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