Single-Colon Indexing for Struct Variables

조회 수: 2 (최근 30일)
AP
AP 2011년 6월 2일
I have an array of structure variable defined as:
my_data(100)=struct('x',[],'y',[]);
x and y are vectors containing coordinates. I am trying to have all the x components of all the elements of the array. I used:
my_data_x_all=my_data(:).x;
But it is not a single variable. Only my_data(1).x is assigned to my_data_x_all and the rest is returned in the MATLAB variable "ans". What am I missing here that I cannot assign all the x components of 100 elements of my_data in the single array my_data_x_all?

채택된 답변

Matt Fig
Matt Fig 2011년 6월 2일
Just to make sure I understand what you did, does this example correctly model your issue?
my_data(3)=struct('x',[],'y',[]);
my_data(1).x = 6;
my_data(2).x = 9;
my_data(3).x = 10;
my_data_x_all = [my_data(:).x]
my_data_x_all =
6 9 10
By the way, this is called the comma separated list behavior of structs. Cell arrays can behave similarly.
  댓글 수: 5
AP
AP 2011년 6월 2일
% ******************************************************************
% ******************************************************************
% ******************************************************************
% ******************************************************************
probably defining a struct is not the best option and maybe cell arrays are better for this job. What do you think?
Walter Roberson
Walter Roberson 2011년 6월 2일
arrayfun(@(S) S.f(:,2), my_data)

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by