Evaluate a function with "evalin"

조회 수: 6 (최근 30일)
Kyriacos
Kyriacos 2012년 1월 10일
I am trying to run a function, and for doing so I need some values/ parameters that I have in the base workspace. Can someone tell me why this does not work? How can I fix it?
x0 = [0.5 ; 0.5 ] ;
evalin('base', 'fsolve(@price2T_ss, x0)') ;
[x, fval, exitflag] = evalin('base', 'fsolve(@price2T_ss,x0)') ;
Error message: ??? Undefined function or variable 'yT_y2'.
... where yT_y2 is a parameter that exists and is already defined in the base workspace, but not in the function's workspace.
Thanks a lot!!
Kyriacos

답변 (2개)

Daniel Shub
Daniel Shub 2012년 1월 10일
This is one of the reasons not to use eval, it makes debugging difficult. I would guess that the price2T_ss function is trying to use yT_y2 and you haven't passed yT_y2 to it. You should also note that I am pretty sure the x0 you are defining in your function workspace is not being by fsolve. Why not just pass yT_y2 to your function so you do not have to do evalin.
  댓글 수: 5
Jan
Jan 2012년 1월 10일
+1. Use EVAL only for these cases:
1. code obfuscation: a kind of tedious copy protection.
2. if you really need to define global variables dynamically: there is no function form like "global('a')"
Btw: avoiding globals is a good programming practize also...
Walter Roberson
Walter Roberson 2012년 1월 11일
There are also some cases having to do with constructing function handles dynamically.

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


Walter Roberson
Walter Roberson 2012년 1월 10일
The fsolve() gets invoked within the base workspace, but the price2T_ss gets invoked from fsolve() or one of its subroutines, and so does not have direct access to the base workspace.
  댓글 수: 3
Walter Roberson
Walter Roberson 2012년 1월 10일
Changing fsolve() would not be good enough: you would need to change price2T_ss .
You should consider using nested functions with shared variables rather than going through the base workspace.
Kyriacos
Kyriacos 2012년 1월 11일
ok, thanks a lot!

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

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by