Open In App

Alternatives To Eval Function in MATLAB

Last Updated : 12 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The eval function is one of MATLAB’s most powerful and flexible commands. Eval is an acronym for evaluating, which is exactly what it does: it evaluates MATLAB expressions. Any command that can be executed from the MATLAB prompt may be executed from a Mfile using eval.

Use of Eval Function:

The Eval function can be used to evaluate an expression that returns a text string or a numeric number. You can create a string and then pass it to the Eval function as if it were an expression. The string expression is evaluated by the Eval function and its value is returned.

Eval Function Syntax:

eval(expression)
eval(expression,catch_expr)
[x1,x2,x3,…] = eval(function(y1,y2,y3,…))

[x1,x2,x3,…] = eval(function(y1,y2,y3,…)) runs a function with 

the arguments y1,y2,y3,… and returns the results in the output variables specified.

Limitations and Alternatives of Eval Functions:

Despite its strength and flexibility, the eval function is not always the ideal option to solve a programming issue. Code that utilizes eval is frequently less effective than code that uses other functions or linguistic constructs, and it is also more challenging to read and debug. 

As illustrated in the following examples, there are recommended alternate ways for many typical uses of eval:

Variable Function Names:

When a variable character vector contains the name of a function, one common application of the eval command is to execute the function. There are two techniques to evaluate functions based on variables that are more efficient than using the eval command, and they are as follows:

  • The @ symbol, along with the str2func function, can be used to generate function handles. Take, for instance, the execution of a function based on a list that is saved in a cell array:

Example 1:

Matlab




% MATLAB example for alternatives
% of Eval functions
examples = {@odedemo,@sunspots,@fitdemo};
n = input('Choose an example (1, 2, or 3): ');
examples{n}()
 
plotFunction = input('state a plotting function: ','s');
data = input('Input data to plot: ');
feval(plotFunction,data)


  • Utilize the eval function in your work. For instance, you could call a plot function (such as plot, bar, or pie) with data that you specify when the program is being run.

Variables With Names in Order:

The eval function is frequently used to create sets of variables like X1, X2,…, and Xn, but this method does not take advantage of MATLAB’s array processing capabilities and is not advised. The best practice is to group similar data into a single array. Use a structure or cell array if the data sets are different in terms of size or kind.

Make a cell array of 20 items, each of which is a numeric array, for instance:

Example 2:

Matlab




numArrays = 20;
X = cell(numArrays,1);
for n = 1:numArrays
    X{n} = magic(n);
end


By indexing with curly brackets, you can access the data in the cell array. Show the tenth element of X, for instance:

X{10}

Output:

 

This call to eval is more elegant and effective than the assignment expression X{n} = magic(n):

eval(['X', int2str(n),' = magic(n)'])     


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads