Best Way to Convert Data in a Structure to an Array

3 visualizaciones (últimos 30 días)
Sonoma Rich
Sonoma Rich el 26 de Feb. de 2021
Editada: Stephen23 el 26 de Feb. de 2021
I am doing the following to convert data in a structure to an array. It seems a little clunky. Is there a better way?
a(1).data = [1 2];
a(2).data = [3 4];
a(3).data = [5 6];
a(4).data = [7 8];
data2 = cell2mat({a.data}');
  2 comentarios
Bob Thompson
Bob Thompson el 26 de Feb. de 2021
You don't need to do the cell2mat conversion, you can just use vertcat.
data2 = vertcat(a(:).data);
I don't have a good way to make your definition of a.data any better.
Stephen23
Stephen23 el 26 de Feb. de 2021
Editada: Stephen23 el 26 de Feb. de 2021
The colon indexing is not required either. This is probably the simplest and most efficient solution:
data2 = vertcat(a.data);

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by