From string to a variable

2 ビュー (過去 30 日間)
federico valenti
federico valenti 2011 年 4 月 18 日
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 )

採用された回答

Jarrod Rivituso
Jarrod Rivituso 2011 年 4 月 18 日
I would use eval
names = {'dog','bike','donut'}
for i = 1:length(names)
eval([names{i} ' = rand;'])
end
  1 件のコメント
federico valenti
federico valenti 2011 年 4 月 18 日
thanks a lot

サインインしてコメントする。

その他の回答 (2 件)

Paulo Silva
Paulo Silva 2011 年 4 月 18 日
assignin('base',char(array(1)),5)
assignin('base',char(array(2)),7)
assignin('base',char(array(3)),54)
  1 件のコメント
federico valenti
federico valenti 2011 年 4 月 18 日
thanks a lot

サインインしてコメントする。


Walter Roberson
Walter Roberson 2011 年 4 月 18 日
It is strongly recommended not to do this. Please see the FAQ

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by