Single-Colon Indexing for Struct Variables

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?

 Respuesta aceptada

Matt Fig
Matt Fig el 2 de Jun. de 2011
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 comentarios

AP
AP el 2 de Jun. de 2011
thank you for your answer. suppose x and y are 1000×1, f be a matrix of 1000×5, and my_data(100)=struct('x',[],'y',[],'f',[]). How can I access for example the second column of f? I tried [my_data(:).f(:,2)] but it does not work.
Matt Fig
Matt Fig el 2 de Jun. de 2011
Your terminology is confusing... If x, y and f are variables in the workspace, calling:
my_data(100) = struct('x',[],'y',[],'f',[])
doesn't put x, y or f into a structure. Are you aware of that?
If so, please give a short example, like the one I gave above, which will address your concern. In your example, you need to put everything in (like I did with my example) that people will need to see what you are seeing... Make just a small example struct as I did then ask what you want to do with it.
AP
AP el 2 de Jun. de 2011
my_data(3) = struct('x',[],'y',[],'f',[])
for i=1:3
my_data(i).x = [1 2 3]*i;
my_data(i).y = [1 2 3]*i;
my_data(i).f = [1 2 3;4 5 6;7 8 9]*i;
end
Actually f is an array which holds the value of a function for each (x,y) and stores its value in different measures. Therefore, [my_data(:).f(:,2)] should mean the value of the function in the second measure for all the data. But I get the following error:
??? Scalar index required for this type of multi-level indexing.
AP
AP el 2 de Jun. de 2011
% ******************************************************************
% ******************************************************************
% ******************************************************************
% ******************************************************************
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 el 2 de Jun. de 2011
arrayfun(@(S) S.f(:,2), my_data)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

AP
el 2 de Jun. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by