hello every body , who know how to name variables dynamically such as X1, x2,X3,...Xn in for loop

 採用された回答

Benjamin Schwabe
Benjamin Schwabe 2012 年 2 月 14 日

1 投票

Hi AYAH,
why do you want to do this, you really might want to use a vector with X(1),X(2),...,X(10), they are dynamic in MatLab, so there is nothing to worry about this. And cases are really rare, in which you want to do what you'd like to have...
However, you can do it by using assignin, e.g. like that:
n=10;
for k=1:n
varname=['X',num2str(k)];
assignin('caller',varname,k)
end

1 件のコメント

AYAH
AYAH 2012 年 2 月 15 日
Thank you very much

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 2 月 14 日

0 投票

3 件のコメント

AYAH
AYAH 2012 年 2 月 16 日
Hi Walter,
I faced problem on how to get the values in for_loop ,such like this:
for i=0:2:10
display X[i] %this statment wrong; how could I display x0 x2,x4,x6,x8
end
And I have another problem that I don't know how to save array of numbers on each variable such as x1=[1 2 4]
x2=[1 7 8 9 7]
Walter Roberson
Walter Roberson 2012 年 2 月 16 日
If you want X0 onward, then
X{2}= [1 2 4];
X{3}= [1 7 8 9 7];
and
for i = 0:2:10
disp(X{i+1})
end
AYAH
AYAH 2012 年 2 月 16 日
Thanks alot

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

カテゴリ

タグ

質問済み:

2012 年 2 月 14 日

編集済み:

2013 年 10 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by