How build an variable output for my function
6 ビュー (過去 30 日間)
古いコメントを表示
Hi
I have a simple function, for exemple:
[ax] = testthat(x,y)
I use this function a lot an need to create a different output name to [ax] for exemple first time the name shold be "a1" second time "b1"... and keep all these output variable availlable.
I try
al=('a':'z').';
[al(3) num2str(1)] = testthat(x,y)
But clearly it is not the good way to make it...
1 件のコメント
Stephen23
2016 年 1 月 28 日
編集済み: Stephen23
2016 年 1 月 30 日
It is certainly "not the good way to make it", because there is no "good way to make it", because using dynamically named variables is very bad way to "make it", which unfortunately beginners seem to fall in love with.
Read this link to know why dynamically named variables is a slow, buggy and obfuscated way to program, and what much better alternatives there are:
Browsers: do not follow the accepted answer, read the link above to know why.
採用された回答
Jonathan Roy
2016 年 1 月 27 日
2 件のコメント
Star Strider
2016 年 1 月 27 日
My pleasure.
I absolutely cannot understand why dynamic variables would be preferable to an array.
その他の回答 (1 件)
Star Strider
2016 年 1 月 27 日
If you are doing it in a loop, subscript it as a cell array instead:
for k1 = ...
a{k1} = testthat(x,y);
end
That keeps everything in one array that you can easily work with elsewhere in your code.
It is not good programming practice to create dynamic variables, such as ‘a1’, ‘a2’ and so forth. They are difficult to work with.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!