How to Index Substructure

I am trying to index a field in a substructure of a structure. The main structure is supermarket
the next structure is produceList
the field i wish to call from produceList is minNumber
I tried supermarket.produceList{i}.minNumber
I need the {i} to allow the for loop to work
for i = 1:length(supermarket.produceList)
It gives me the error Dot indexing is not supported for this type of variable.
I know i can index with specific elements in the structure such as supermarket.Apples.minNumber. However, this prevents me from testing it for each element of substructure produceList

回答 (1 件)

Guillaume
Guillaume 2018 年 12 月 13 日

0 投票

I'd recommend you use numel instead of length, it's safer.
Structure indexing uses () not {} (that's to acces cell arrays and tables content), so:
supermarket.produceList(i).minNumber
Your blurb about supermarket.Apples is confusing. Is the substructure a structure array named produceList or a bunch of scalar structures with different names? The latter would require a completely different approach.

カテゴリ

ヘルプ センター および File ExchangeDebugging and Improving Code についてさらに検索

製品

質問済み:

2018 年 12 月 13 日

回答済み:

2018 年 12 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by