'inline' command at 'for' loop... can we?
    조회 수: 17 (최근 30일)
  
       이전 댓글 표시
    
can we put 'inline' command at 'for' loop
I want to save several inline function at an array... Available?
댓글 수: 0
채택된 답변
  Friedrich
    
 2013년 4월 17일
        
      편집: Friedrich
    
 2013년 4월 17일
  
      Hi,
yes it should be possible but its better to create some Anonymous Functions instead of using inline, e.g.
f = cell(10,1);
for i=1:10
    f{i} = @(x) x*i;
end
The output of f looks a bit strange because it seems to be the same function handle all the time, but it isn't:
>> f{1}(1)
ans =
       1
>> f{2}(1)
ans =
       2
>> f{3}(1)
ans =
       3
>> f{3}(4)
ans =
      12
Or:
f = cell(2,1);
for i=1:2
    tmp = inputdlg('enter function handle')
    f{i} = eval(tmp{:})
end
And enter things like:
   @(x)x^2
   @(x)x-3
Make the call to eval fail proof!!
댓글 수: 1
  Daniel Shub
      
      
 2013년 4월 19일
				There is no need to hit it with the evil eval hammer, str2func seem perfectly designed for this purpose with hopefully a lot less side effects
추가 답변 (2개)
  Masoud Ghanbari
 2013년 4월 17일
        댓글 수: 12
  Friedrich
    
 2013년 4월 22일
				You need to zuse {} instead of () when you call eval. inputdlg gives back a cell. Also don't use i to index into it. This should work:
f{i} = eval(user_input{1})
참고 항목
카테고리
				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!