How do I create variable names from string variables
Mostrar comentarios más antiguos
If I have 2 variables, ID has a column of strings like 'height' or 'speed', data has a column of corresponding variables.
ie
ID = 'height' 'height' 'height' 'speed' 'speed'
data = 10;10;7;2.4;2.1
How do I create the variable 'height' and a variable speed with the corresponding data.
ie
height = 10;10;7
speed = 2.4;2.1
Respuesta aceptada
Más respuestas (1)
Andrei Bobrov
el 13 de Ag. de 2013
Editada: Andrei Bobrov
el 13 de Ag. de 2013
ID = {'height' 'height' 'height' 'speed' 'speed'};
data = [10;10;7;2.4;2.1];
[i0,i1,i1] = unique(ID);
vars = cell2struct(accumarray(i1(:),data(:),[],@(x){x}),i0,1);
1 comentario
Matt Kindig
el 15 de Ag. de 2013
I should have figured there was an easy way to do this using accumarray. Try as I might, I still can't get the hang of that function.
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!