add a new field to structure
45 ビュー (過去 30 日間)
古いコメントを表示
Hi all friends
I have a structure variable; this variable is defined as follows:
Student structure; consists of the following fields
Name
Age
Suppose that I fill it with data (for example five students)
Later I want to add another field (for example degree field) to student structure
How can I do that and fill the degree field with data
Please help me
0 件のコメント
採用された回答
Wayne King
2013 年 12 月 4 日
編集済み: Wayne King
2013 年 12 月 4 日
teststruct = struct('names',{'bob','dave','sara'});
% now add field called data
[teststruct(:).data] = deal(randn(3,1));
fieldnames(teststruct)
2 件のコメント
Kyle Wright
2017 年 12 月 19 日
編集済み: Kyle Wright
2017 年 12 月 19 日
Is this answer working as intended?
I would assume that you used...
deal(randn(3,1))
...using a "3" so that it would match the length of the struct as it is. However, this code puts the 3x1 array into EACH of the (.data) fields, rather than distributing one of each of the vector to one of each of the (.data) fields.
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!