필터 지우기
필터 지우기

how to index a cell array

조회 수: 1 (최근 30일)
li yan
li yan 2018년 5월 23일
댓글: li yan 2018년 5월 23일
Hi, I encountered a Matlab problem again. I want to access all struct field elements, which is an element of a cell array, at one time, not using loop. Such as:
s{1} = struct('ID',1,'other field',...);
s{2} = struct('ID',2,'other field',...);
I want to access all ID values in the cell s, I write the code like this:
IDs = [s{:}.ID];
This does not work, how to access all ID values not using for loop, please?
  댓글 수: 2
Stephen23
Stephen23 2018년 5월 23일
If you used just one non-scalar structure (rather than lots of scalar structure in a cell array) then the solution would be very simple:
S(1) = struct('ID',1,'other field',...);
S(2) = struct('ID',2,'other field',...);
[S.ID]
Simpler, more efficient code through better data design. Why make your code more complex than this?
li yan
li yan 2018년 5월 23일
Thank for you answer.
I'm doing information fusion with matlab, the track numbers vary dynamic, a cell struct is convenient to store track information.

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

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 23일
You can do this in Two steps
temp = [s{:}];
allIDValues = [temp.ID];
  댓글 수: 2
Stephen23
Stephen23 2018년 5월 23일
Note that in Ameer Hamza's answer the first line of code converts your cell array of scalar structures into one non-scalar structure. If you had stored your data in a non-scalar structure in the first place, then you would not need this extra step.
li yan
li yan 2018년 5월 23일
thank you for your answer. I will try it

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

추가 답변 (0개)

카테고리

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