MATLAB Answers

Is there an alternative to eval for delayed evaluation?

2 views (last 30 days)
Marc Lalancette
Marc Lalancette on 20 Sep 2017
Edited: Stephen Cobeldick on 24 Sep 2017
I'm trying to make an input argument parsing function for use with functions that have many optional input arguments. I accept these as a struct or label/value pairs (via varargin), and I pass it along with a variable containing validation anonymous functions for each argument (e.g. @isnumeric) and another with default values:
function ParseVarargin(varargin, Validation, Defaults)
This function goes through the user-supplied arguments, validates each and if an argument is not supplied, it uses the default value. This is done with assignin('caller',). Now the issue is that some defaults may be expressions that depend on other input arguments (that were processed before of course). The only way I found to do this correctly is to use evalin('caller', ), but that means I must provide my default expressions to ParseVarargin as strings. I really don't like that though as this hides my code in a string. I would have liked instead to make an anonymous function that only gets evaluated when it is called, thus using the user-supplied values. I'm pretty sure there's no alternative, but thought I'd ask the community.
  1 Comment
Stephen Cobeldick
Stephen Cobeldick on 20 Sep 2017
Your whole concept relies on magically making variables appear and disappear in different workspaces: note that this will inherently be complex, buggier, hard to debug, and much slower than simply passing variables as input/output arguments.
Then on top of that you wish to do some magic with function handles and evaluating them in other workspaces with some "delay"... ouch. It might be worth considering some of the effects that this will have on your code (the points made about eval apply to assignin too, with the added disadvantage that you are messing around in more than one workspace):
You really should read this thread too:

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 20 Sep 2017
Edited: John D'Errico on 20 Sep 2017
This is absolutely insane! Why would you use assignin here? Just return the arguments as a struct.
Set up your code to take a struct full of parameters. The values of those parameters initially are set to the default. So each field of this struct contains the default value initially.
Then pass in the struct, plus all property/value pairs to a parsing tool. If it sees a property, it checks the corresponding field of the struct, overwriting the default value held there. If there was no field with that name, just create one.
Then return the same struct to the caller function.
Now all of your parameters are contained in one simple struct. Easy to pass around. Easy to use. Easy to access any parameter you may happen to need.
Absolutely no reason to use assignin. Of course, if you prefer buggy, inefficient code, where any bugs will be pure hell to track down, then continue to use assignin.
Jan on 21 Sep 2017
I agree with John and Steven. The automatic creation by assignin is a shot in your knee. Your automatic parsing of inputs can slow down the code remarkably (factor 2 or 20) and makes it much harder to debug and maintain. Developing super-smart methods for intelligent parsing is not efficient or useful. I like to keep it simply stupid, because this allows for concentrating of the function to be calculated.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!