2

I just read the following article from MathWorks which describes why it is important to avoid the eval function and lists alternatives to many of eval's common uses.

After reading the article, I have the impression that the eval function is neither useful nor necessary. So, my question is this: When is the eval function necessary?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Bryan F
  • 830
  • 8
  • 14
  • The situation is similar to [`goto` in C++ and other mainstream languages](http://en.cppreference.com/w/cpp/language/goto) and some other low-level constructs that are too easy to abuse. It's available, but its use is very bad practice, and except for very *very* rare cases they should be avoided. Like, as long as there is *any* solution that doesn't involve using it. – Andras Deak -- Слава Україні May 23 '16 at 21:06
  • 3
    Matlab's Symbolic Math toolbox uses `eval` extensively under the hood (and used to rely on it directly for the now mostly deprecated string syntax.). Symbolic math is much like a separate interpreted language. – horchler May 23 '16 at 21:48
  • `str2num` also uses `eval` under the hood (as does 100s of files if you do a search in your matlab root folder) – matlabgui May 24 '16 at 07:59

2 Answers2

7

I have found only one useful case for eval, and then the evalc variety: when calling a function with built-in command line call back (e.g. lines without ; or with disp calls), which you cannot modify. For instance when you got some obfuscated function that dumps heaps of stuff to your command window. In that case it's best to try and obtain the source code to modify that to your needs, as using evalc will mess up your performance. Otherwise, I have not found a case where eval is the best solution.

I wrote an extensive answer detailing why you should try to avoid eval as much as possible here: How to put these images together?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
-3

I have already used eval when trying to create multiple arrays with different names. This is not really recommended, but it worked for my specific application. For example, if I wanted to have N matrices with the specific names "matrix1" "matrix2" .. "matrixN" , one solution would be to manually type these in as "matrix1 = something" ... "matrixN = somethingelse". If N is really large, this is not ideal. Using eval , you could set up a for loop that would change the name of the matrix on every loop, and calculate some value based on that same N value.

Of course, ideally saving them in to a cell would be better, but I needed the arrays in the format I described.

jerpint
  • 399
  • 2
  • 16
  • 2
    If you need your variables to be named Dynamically you have a serious problem somewhere in your code. Having Dynamically named variables also means you have to read them out dynamically, probably by using `eval` as well if you already used that. Better to use a cell directly. – Adriaan May 25 '16 at 06:33
  • Yes, as I mentioned , that usage is not at all recommended, it was to satisfy some criteria to some already poorly structured code. – jerpint May 25 '16 at 12:40