MATLAB Answers

add a new field to structure

577 ビュー (過去 30 日間)
Majid Al-Sirafi
Majid Al-Sirafi 2013 年 12 月 4 日
編集済み: Stephen 2017 年 12 月 19 日
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

採用された回答

Wayne King
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 件のコメント
Stephen
Stephen 2017 年 12 月 19 日
C = num2cell(randn(3,1));
[teststruct(:).data] = deal(C{:})
How this works is explained in the MATLAB documentation:

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

その他の回答 (1 件)

Majid Al-Sirafi
Majid Al-Sirafi 2013 年 12 月 4 日
thank you sir, that is I want
best regards

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by