Explanation

MATLAB is unable to analyze code within statements called by eval. This prevents MATLAB from performing any performance optimization and limits the guidance that the editor is able to provide. This makes code less efficient and difficult to debug.

Furthermore, concatenated character vectors within an eval statement are often difficult to read. This makes code less clear and causes issues with sharing and maintaining code.


Suggested Action

Structure fields or object properties can be accessed using dynamic field names. The example shows how code using eval to assign numbered field names can be refactored to use dynamic field names.

OldNew
for i = 1:5
    eval(['handles.axes', num2str(i)]) 
end
for i = 1:5
    handles.("axes" + num2str(i))
end

MATLAB is able to detect that fields of handles will be accessed and this allows performance optimization.

For more information, see Generate Field Names from Variables .