Changing function with Iteration
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi all I want to call function/script iteratively as I made mutants of a program and each mutant ends with a iteration number. The problem is that I need to change function name iteratively with each iteration of matlab code. Please help me out. My sample mutants are ArithOper1(input),ArithOper2(input),ArithOper3(input),........
Thanks you in Advance
amir
댓글 수: 0
채택된 답변
Matt Tearle
2012년 4월 19일
So you have a bunch of functions called ArithOper1.m, ArithOper2.m, ArithOper3.m, etc? If there's a small enough number of these, you can call the appropriate one using a simple switch construct
switch n
case 1
% call ArithOper1
case 2
% call ArithOper2
end
If this is unwieldy, you can use eval:
fnm = ['ArithOper',num2str(n)];
str = ['z = ',fnm,'(x,y);']; % z = ArithOperN(x,y)
eval(str);
댓글 수: 2
Matt Tearle
2012년 4월 20일
You need to store the output somehow. eval just evaluates a string as a command, so you're executing the command "z = ArithOperN(x);" (with N being filled in with a given value). So you just keep overwriting z in the loop. What do you want to do with the output? Store it somewhere? Just display it? Whatever it is, change your string (str) to make the appropriate command. Equivalently, add some commands after "eval(str)" that does something with the result which is currently stored in z.
추가 답변 (1개)
Sean de Wolski
2012년 4월 19일
- FAQ a1 a2 an
- doc persistent %use persistent variables
- doc switch %make a decision based on something
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!