Evaluate a function with "evalin"

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

Answers (2)

Daniel Shub
Daniel Shub on 10 Jan 2012
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 Comments

Hi Daniel,
... and thanks for your answer. Obviously, I could pass the parameter(s) directly to the function, I just simply would like to do it automatically. So, if there is a way that the function could be run directly in the base workspace, or use values from it, it would be better for me. That's why I post the question, otherwise what I did for the moment was to include this command in the function mat-file itself:
yT_y2 = evalin('base', 'yT_y2') ;
and it works. But I prefer to do it automatically.
Kyriacos
The eval, evalin, and assignin functions are powerful and probably will let you do whatever you want "automatically". That said, I cannot imagine a situation in which your code is relevant and that you would want to do it. You are already experiencing problems with it. Please, for your own sanity, pass the arguments that you need into your function and return only the arguments that you need and get rid of all uses of eval/evalin.
+1. do not use eval for this (or pretty much anything...), its slow, error prone, will lead to problems/bugs etc...
+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...
There are also some cases having to do with constructing function handles dynamically.

Sign in to comment.

Walter Roberson
Walter Roberson on 10 Jan 2012
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 Comments

So you are saying that there is no particular way to do it unless I change the fsolve routine itself? Which of course I wouldn't do...
Kyriacos
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.
ok, thanks a lot!

Sign in to comment.

Categories

Tags

Asked:

on 10 Jan 2012

Community Treasure Hunt

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

Start Hunting!