Specifying elements of a structure array that have different fields

조회 수: 1 (최근 30일)
Martin
Martin 2014년 6월 5일
댓글: José-Luis 2014년 6월 5일
Hello -
I'm using a structure array to describe a set of particles that can have very different properties. I've found that by creating a structure field for each element of the array, I can then add specific fields that may not be shared by all members of the array:
particle(1).type = 'human';
particle(1).properties.name = 'Barney';
particle(1).properties.gender = 'm';
particle(1).properties.weight = 190.0;
particle(2).type = 'vehicle';
particle(2).properties.model = 'Camry';
particle(2).properties.weight = 3000.0;
particle(2).properties.length = 14.0;
Is this permissible to do? Thanks.

답변 (1개)

José-Luis
José-Luis 2014년 6월 5일
편집: José-Luis 2014년 6월 5일
To get a logical index into all 'human':
humanIdx = cellfun(@(x) strcmp('human',x),{particle(:).type})
Please accept an answer if it helped you.
  댓글 수: 2
Martin
Martin 2014년 6월 5일
I think I was confusing. I am just wondering if it is OK to build such structure arrays with different fields in a substructure, or whether this will cause some kind of problem
José-Luis
José-Luis 2014년 6월 5일
Yes you can. Accessing stuff will lead to tricky syntax, but is doable as shown above.
What is your ultimate purpose? Build a database? If so, then Matlab is really not the right tool for the job.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by