How to access sub-properties from an object array?

조회 수: 2 (최근 30일)
Aidan K
Aidan K 2018년 10월 27일
댓글: Aidan K 2018년 10월 28일
So I have an array of objects and I ideally want to return an array of sub-properties two layers deep (if that makes sense). For example, I would like:
array=person.eye.color
to return all of the person eye colors into one array. I can return an array of eye objects but not the eye colors themselves.
I get the error: "Expected one output from a curly brace or dot indexing expression, but there were 3 results."
I'm not a very experienced programmer or very familiar with Matlab's structure, any advice? Thanks.

답변 (1개)

per isakson
per isakson 2018년 10월 27일
Assuptions
  • person is the array of objects
  • person has a property named, 'eye', the value of which is an eye-object
Answer
There is no one-liner that does what you want. You have to loop over all persons of the array.
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 10월 27일
array = arrayfun(@(P) P.eye.color, person); %hidden loop
Aidan K
Aidan K 2018년 10월 28일
Thanks. Seemed to do the trick.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by