Setting variable names from a character array

hi,
I have a character array, for example:
vars = ['X';'Y']
I want to assign the name of an element in vars to a double array, such as:
for i=1:2
char(vars(i,:)) = rand(10,1)
end
Unfortunately this does not work as I am obviously attempting to equate a character array to a double array of different length. Instead of course what I was attempting to do is name a double array from a predefined list.
Please help!
thx

 채택된 답변

Jan
Jan 2011년 9월 23일

0 개 추천

Do not do this. Creating variables dynamically a is common and frequent source of errors. In addition is is slow. See:
Use either an array, cell array or struct:
vars = {'X', 'Y'};
S = [];
for i = 1:2
S.(vars{i}) = rand(10,1);
end
Or:
C = {};
for i = 1:2
C{i} = rand(10,1);
end

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품

질문:

gfb
2011년 9월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by