Access value in cell arrays
Mostrar comentarios más antiguos
A{1,1}.str = 1;
A{2,1}.str = 2;
... (so on)
A{10,1}.str = 10;
Can I say:
B = A{:,1}.str;
so that:
B=[1 2 3 4 5 6 7 8 9 10];
Thanks very much
2 comentarios
per isakson
el 29 de Jun. de 2013
Is A supposed to be a cell arrays of structures?
A field named "str" holding a numerical value isn't that confusing?
TN
el 29 de Jun. de 2013
Respuesta aceptada
Más respuestas (2)
No. You can do this instead
A(1).str = 1;
A(2).str = 2;
...
A(10).str = 10;
B=[A(:).str]
3 comentarios
TN
el 29 de Jun. de 2013
Matt J
el 29 de Jun. de 2013
It would not make sense to hold structures having the same fields inside cells. It just makes them harder to get to (as you've discovered).
per isakson
el 29 de Jun. de 2013
Editada: per isakson
el 29 de Jun. de 2013
I agree.
However, for some reason the cell array may contain structures with only some fields in common.
James Tursa
el 29 de Jun. de 2013
Another variation:
x = [A{:,1}];
B = [x.str];
2 comentarios
TN
el 29 de Jun. de 2013
Matt J
el 30 de Jun. de 2013
TN, if James solution works for you, there is really no reason to be carrying around A. You may as well just use x. As per said, it might make sense if A{i} were structs with different fields, but James' approach will not work if that is the case.
Categorías
Más información sobre Cell Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!