How to convert a matrix to a structure array?
Mostrar comentarios más antiguos
I am working with data that is in a matrix but would like to easily convert it to a structure array. At the moment I am using the "table" and "table2struct" commands as per the simple example below but I feel that there should be a more efficient method that I'm missing as I will likely have far more than three variables and the current method I'm using seems inefficient. Any assistance would be appreciated!
M=randn(5,3);
T=table(M(:,1),M(:,2),M(:,3),'VariableNames',{'Var1' 'Var2' 'Var3'});
S=table2struct(T);
2 comentarios
Steven Lord
el 18 de Jul. de 2018
Why do you need to convert the matrix into a struct array? How are you planning to use the fields in the struct later in your code?
Personally most of the time I'd just leave it as M and use indexing to extract the columns as I need them.
Hannah
el 19 de Jul. de 2018
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Structures 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!