How to build a vector with dynamic variables?

I have variables :
q1 = 10;
q2 = 20;
q3 = 3;
q4 = 4;
q5 = 55;
...
qN = x;
and I want to put them in a vector Q that i created like :
Q = zeros(N,1);
How can I insert each variable in the vector at the correspondig place with a loop. Something like that :
for i = 1:N
Q(i)= % here qi
end

 採用された回答

Matt J
Matt J 2018 年 11 月 19 日
編集済み: Matt J 2018 年 11 月 19 日

0 投票

I hope you're doing this because you have now realized that it was a bad idea to create q1...qN to begin with and are seeking to undo the damage.
>> Q=arrayfun(@(i) evalin('caller',['q' num2str(i) ';']) , 1:5)
Q =
10 20 3 4 55

1 件のコメント

Guillaume Senger
Guillaume Senger 2018 年 11 月 19 日
Thank you very much, it works perfectly!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2018 年 11 月 19 日

編集済み:

2018 年 11 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by