필터 지우기
필터 지우기

How build an variable output for my function

조회 수: 1 (최근 30일)
Jonathan Roy
Jonathan Roy 2016년 1월 27일
편집: Stephen23 2016년 1월 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
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
Jonathan Roy 2016년 1월 27일
I know it is not the best way... but I realy need to work that way.
I find a solution, I use eval() an other not recommand method but it work for my.
Thank you Star Strider for your fast help!
  댓글 수: 2
Star Strider
Star Strider 2016년 1월 27일
My pleasure.
I absolutely cannot understand why dynamic variables would be preferable to an array.
Stephen23
Stephen23 2016년 1월 28일
편집: Stephen23 2016년 1월 28일
@Jonathan Roy: read this to know why dynamically named variables are a really bad way to program (no matter how much beginners love them):
You should have used either a cell array (as per Start Strider's answer) or a multidimensional array.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Star Strider
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by