Setting up nested structure and adding an element

4 ビュー (過去 30 日間)
Bryan Wilson
Bryan Wilson 2016 年 11 月 3 日
編集済み: Bryan Wilson 2016 年 11 月 3 日
I'm struggling to set up my nested structure.
data1 = struct('x',[1,2,3],'y',[4,5,6]); %data structure
b = struct('name',{'Bob'},'data',{data1});
'data' is an array of structures like data1. And at a time in the future, I need to add more data elements to Bob.
b.data(2) = data2
I feel like I'm missing a basic concepts here, and I'm getting nowhere reading through the MatLab Help.
  2 件のコメント
Stephen23
Stephen23 2016 年 11 月 3 日
編集済み: Stephen23 2016 年 11 月 3 日
Your question is not clear. 'Bob' is a string, and not a fieldname, and you do not refer to it at all in your example of what you are trying to do, so it is not clear what relevance your statement "I need to add more data elements to Bob" has.
In any case this code works for me without error:
>> data1 = struct('x',1:3,'y',4:6);
>> data2 = struct('x',0:2,'y',3:5);
>> b = struct('bob',{'hi'}, 'data',{data1});
>> b.data(2) = data2;
If you are getting an error or something unexpected, then you need to tell us exactly that message or output. We cannot read your computer screen, nor your mind.
Structures are accessed by indexing and fieldnames, which you seem to understand, so it is not clear what your problem is. Please tell us what you are trying to do, and exactly what result you expect.
You can read all about structures here:
Bryan Wilson
Bryan Wilson 2016 年 11 月 3 日
編集済み: Bryan Wilson 2016 年 11 月 3 日
What do ya' know, it does work! I had a errors in my actual code that I inadvertently fixed when I simplified things to post my question. As for adding elements to Bob...I was just referring to my structure b by it's name, 'Bob'.
Thanks for your help.

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

回答 (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