This function can be an excellent tool, but its use should be reserved for exceptional cases. If possible, alternative options should be chosen, as their use can cause several inconveniences and problems during execution.
Below, we take a detailed look at the syntax, input and output arguments, and the data this function accepts. You will also find a detailed description of this function and its solutions, with code snippets and images explaining how to use it step by step.
MATLAB eval Function Syntax
Description of the MATLAB eval Function
MATLAB’s eval function evaluates a given expression in “exp” and returns the result in “a”. Conceptually, the eval() function converts the text you type into the input string into code that executes and returns results. This function provides excellent flexibility because it allows multiple output results, and its input arguments can evaluate expressions and functions from the MATLAB library and those created by the programmer. The input argument to this function must be a string containing the expression’s explicit expression to be evaluated or implicitly by a character vector. When we call the eval() function with expressions that return multiple results, they must be enclosed in square brackets and separated by commas.
When using eval() to evaluate expressions or functions not previously specified by the programmer, remember that anything entered as a string becomes part of the code. Improper handling of this input can lead to errors or exceptions.
In the following examples, we will evaluate various functions and input expressions with the eval() function and implement their use. We will also show you workarounds that allow you to achieve the same results without using this function, which provides workarounds and is very flexible. However, this can also lead to significant drawbacks.
How To Evaluate a Simple Expression With MATLAB eval() Function
Next, we will see several simple examples where we implement the eval function to evaluate expressions or calls to simple functions. The following snippet evaluates a division expression of a by b.
b = 5;
eval ( 'a ./ b' )
In this case, eval() returns the result of dividing a by b.
In the following snippet, eval() evaluates an expression that uses a MATLAB library function to produce an array of ones:
eval ( 'ones (a)' )
These are the simplest ways to use the function. As we can see, the string character we send in the input arguments is converted into lines of code executed to return a result.
Next, we will create an application that evaluates expressions and displays the results on the screen.
Application For Evaluating Expressions With the eval Function of MATLAB
In this example, we will create a simple console application to evaluate expressions with eval(). This simple tool can quickly parse and evaluate the expressions we specify as input arguments when using this function. This application consists of four lines of code. It is an infinite loop that first uses the input() function to input the string that will be the expression to be evaluated. This string is sent as an input argument to eval().
Below is the code for this application. Create a script, paste this code, and press run. Then, at the prompt, type the expression you want to evaluate and press enter. Remember that eval() accepts only strings in its input arguments, so expressions should be enclosed in single quotes. To close the application, press Ctrl+c.
exp_1 = input ('Enter an expression. To exit application press Ctrl+c.');
eval ( exp_1 )
end
As shown in the following figure, this application uses the eval() function and returns the result of the expression we enter via the command console:
Risks To Using the eval() Function in MATLAB
The eval() function offers interesting aspects in terms of its flexibility. Still, it can also have several drawbacks that generate errors, serious exceptions in the system, or erroneous results, so its use should be limited. Alternatives that provide more security and robustness should be implemented instead.
As we have seen in the description and previous examples, any text we enter in the form of a string into the input arguments of eval() will be converted into code and executed, which it is conceptually a gateway or input interface to enter user commands into an already running program. We need to pay particular attention to this because if we leave this door open, we do not have complete or predetermined control over what will be executed in our system at some point in the first place. In addition, expressions can often conflict with the names of already defined variables and produce incorrect results or system errors. These errors can also occur due to incompatibilities in data types between the variables of the expression being evaluated and the variables defined in the program.
Alternatives to the MATLAB Evaluation Function
There are several alternatives to avoid using the eval() function. One is to create a function that resolves the expression instead of evaluating it with eval(). This gives us the possibility to work with already preset variables and also to control what kind of data the variables of this expression should accept, which prevents errors or exceptions.
Conclusion
In this article, we showed you how to use the eval() function to evaluate strings and convert them into executable expressions that return a result. We also showed you some of the problems that can arise from using this function and the alternatives that MATLAB offers to achieve the same result using more reliable methods. For a better explanation, we have created some practical examples that include fragments from the image code that show this function’s implementation in the MATLAB environment. We have also created a practical console application that you can use to test the expressions that we will use as input arguments when implementing this function.