필터 지우기
필터 지우기

From string to a variable

조회 수: 1 (최근 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

추가 답변 (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)

Walter Roberson
Walter Roberson 2011년 4월 18일
It is strongly recommended not to do this. Please see the FAQ

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by