From string to a variable

Hi everybody, i've a string array array={'dog', 'bike', 'donut'} for example from here i need, for each word, a numeric variable with the same name
dog = 5;
bike = 7;
donut = 54;
(doesn't matter the numbers the most importan thing is to create the variable from the string in array )

 Respuesta aceptada

Jarrod Rivituso
Jarrod Rivituso el 18 de Abr. de 2011

0 votos

I would use eval
names = {'dog','bike','donut'}
for i = 1:length(names)
eval([names{i} ' = rand;'])
end

Más respuestas (2)

Paulo Silva
Paulo Silva el 18 de Abr. de 2011

0 votos

assignin('base',char(array(1)),5)
assignin('base',char(array(2)),7)
assignin('base',char(array(3)),54)
Walter Roberson
Walter Roberson el 18 de Abr. de 2011

0 votos

It is strongly recommended not to do this. Please see the FAQ

Categorías

Más información sobre Elementary Math en Centro de ayuda 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