How can i create variables from a string?
2 ビュー (過去 30 日間)
古いコメントを表示
Hi! I want to create an array of variables m1,m2,m3,....,mn, that are created by join "m" with array "1,2,3,...,n" I wrote a code (below), and i want to after run it, Matlab returns: m1=1 m2=5
M=[1 2 3;
4 5 6;
7 8 9];
mch=diag(M,0)';
x=2;
for i=1:x
ha=num2str(i);
m(ha)= mch(i);
end
Thank you so much!
1 件のコメント
採用された回答
KSSV
2017 年 5 月 18 日
Why you need a dynamic variable naming? This is not suggested and you should avoid.
You have matrix already and you can happily call them:
M=[1 2 3;
4 5 6;
7 8 9];
M(1) % is M1
M(2) % is M2
M(6) % is M6
.
.
.M(i) % is Mi where i <= numel(M)
3 件のコメント
Sami Aldalahmeh
2019 年 10 月 7 日
I believe that you should answer the question if you can not discourage the user. Giving advice in a polite way, on the other hand, is always welcomed.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!