Sending a Function Handle to a Function

조회 수: 3 (최근 30일)
Naty S
Naty S 2013년 5월 7일
Hey all I am trying to a send a function handle to a different function. Inside the function handle i have a got a variable which i am changing every loop. The problem is that when i send the function handle to a different function the variable is no longer known to the sub-function.
for it = 1 : num
ene(it)
BL.d=ene(it)*0.54;
dd=double(BL.d);
height=@(z)(dd);
height_str=func2str(height);
% ene(it)
% retp
if retp(2)~=inf,
ret = fix_point(BL,ctrl,height_str, retp, period);
end
end
So i get the following error
  • |Undefined function or variable 'dd'.
Error in @(z)(dd)|*
Any suggestions? Thanks! Naty

채택된 답변

Walter Roberson
Walter Roberson 2013년 5월 7일
Where are you executing the function?
You are using func2str(), which changes the function handle to text, and you are passing the resulting string in to another routine. Are you evaluating that function string in the other routine? If you are then the conversion to string is what is doing you in: the reference to the variable is being converted to the name of the variable.

추가 답변 (1개)

Naty S
Naty S 2013년 5월 7일
So i can't send a string, must i send a function? That's where the error occurs:
function ret = bl_poincare_map(poin_sec,ctrl,height ,BowLeg, period)
fd=str2func(height);
d=fd(BowLeg.x(2));
Thanks for the response Naty
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 5월 7일
Yes, you must send the function handle itself. Then if "height" is the function handle, invoke
d = height(BowLeg.x(2))

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by