How do I use EVALIN to call a function and access variables from a workspace?

조회 수: 4 (최근 30일)
I want to call a function that I have created already in a part of my script.
The function should use the variables from earlier part of my script (the variables that I can see on the Workspace window).
So I wrote:
vFollowing = evalin(ws,'fzero(@f,[0 100])');
and I got this error: ??? Undefined function or variable 'ws'.
Could anybody help me with this? I have no idea where is workspace and how can call the variables from workspace window.

답변 (2개)

Walter Roberson
Walter Roberson 2011년 1월 28일
We deduce from the fact that used @f rather than f in your fzero call that f is the name of a function rather than being the handle of an anonymous function. If that is correct, that f is a function, then in order to have a hope of working, f would have to consist of a call to evalin specifying 'caller' as the workspace, and the function so invoked would have to specifically evalin() 'caller' or assignin() 'caller' the variables it wanted to use. The caller of f would be fzero() and the caller of fzero would be the workspace that had the fzero() call -- maybe, but it could be that fzero calls one or more internal routines that would have to be worked around...
If you do not need to change variables in your current workspace, and only need to read them, then you would be better off using an anonymous function that passed the needed variables to a version of f re-written to accept the variables as arguments:
fzero(@(x) f(x,p,q,r), [1 100])
If you need to be able to change the variables in your current workspace, not just read them, then you are probably using poor coding practices.

Sebastian
Sebastian 2011년 1월 28일
For evalin() ws needs to be a string. Options are 'base' or 'caller' to denote the MATLAB base workspace or the workspace of the caller function. Please see the details when you run
>> doc evalin
From your description I assume you could use the eval() function instead which evaluates your string in the current workspace.
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 1월 28일
evalin() can also accept the output of symengine() as its first argument, an output that is not certain to be a string.

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by