Effacer les filtres
Effacer les filtres

From string to a variable

3 vues (au cours des 30 derniers jours)
federico valenti
federico valenti le 18 Avr 2011
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 )

Réponse acceptée

Jarrod Rivituso
Jarrod Rivituso le 18 Avr 2011
I would use eval
names = {'dog','bike','donut'}
for i = 1:length(names)
eval([names{i} ' = rand;'])
end
  1 commentaire
federico valenti
federico valenti le 18 Avr 2011
thanks a lot

Connectez-vous pour commenter.

Plus de réponses (2)

Paulo Silva
Paulo Silva le 18 Avr 2011
assignin('base',char(array(1)),5)
assignin('base',char(array(2)),7)
assignin('base',char(array(3)),54)
  1 commentaire
federico valenti
federico valenti le 18 Avr 2011
thanks a lot

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 18 Avr 2011
It is strongly recommended not to do this. Please see the FAQ

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by