I was encountered with usage of function eval(expression) in somebody else’s code in matlab:
for example:
for n = 1 : 4
sn = int2str( n) ;
eval( [ 'saveas( fig' sn ', [ sName' sn ' ], ''fig'' ) ' ] );
end
MathWorks stuff in Matlab Help pointed out that:
Many common uses of the eval function are less efficient and are more difficult to read and debug than other MATLAB functions and language constructs.
After this, I find usage of this function in many other program languages, so as Python, JavaScript, PHP.
So I have a few questions:
- Will usage of this function be enfluenced on the performance of my code?
- If it will slow down execution, why does it occur?
- If it slow down execution every time when called, what reason for use this function in principle?
evallines cannot be optimized.fig1throughfig4. If they had been stored in an array calledfig, i.e.fig(1)etc,evalwould have been unnecessary.EDIT Here are two excellent articles by Loren Shure about why
evalshould be avoided in Matlab. Evading eval, and More on eval.