Azzera filtri
Azzera filtri

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

9 visualizzazioni (ultimi 30 giorni)
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.

Risposte (2)

Walter Roberson
Walter Roberson il 28 Gen 2011
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 il 28 Gen 2011
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 Commento
Walter Roberson
Walter Roberson il 28 Gen 2011
evalin() can also accept the output of symengine() as its first argument, an output that is not certain to be a string.

Accedi per commentare.


Community Treasure Hunt

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

Start Hunting!

Translated by