Home » Matlab » The Use Of ‘eval()’ Command In Matlab® With Examples

The Use Of ‘eval()’ Command In Matlab® With Examples

In Matlab®, eval() is a very versatile command that you can easily use for lots of purposes. Here, we show you how to use the eval() command in various examples done in the Matlab® command window.

How To Use ‘eval()’ Command In Matlab®?

MATLAB For Beginners: A Gentle Approach

If you are interested to learn Matlab® at an engineering level, click on the given link or the ‘Shop Now’ button to check the recommended book by Mechanicalland, from Amazon!

Take a look at the below example to understand ‘eval()’ command in Matlab®.

>> a = 1:10;
b = eval('20*a+233')

b =

   253   273   293   313   333   353   373   393   413   433

>> 

As you see in the above example, you can use the eval() command but typing functions inside it. ‘a’ is a vector and this vector has ten elements inside it, starting from 1 to 10.

We typed a function in eval() command inside quotes, in which the variable is ‘a’. We assigned this eval() to ‘b’. ‘b’ vector will take the new values of ‘a’ according to the function inside it.

This is an another example about the use of eval() command in Matlab®.

YOU CAN LEARN Matlab® IN MECHANICAL BASE; Click And Start To Learn Matlab®!

for a = 1:10
 eval(['x' num2str(a) ' = [a+1 ; 2*a ; a/3]'])
end
    

x1 =

    2.0000
    2.0000
    0.3333


x2 =

    3.0000
    4.0000
    0.6667


x3 =

     4
     6
     1


x4 =

    5.0000
    8.0000
    1.3333


x5 =

    6.0000
   10.0000
    1.6667


x6 =

     7
    12
     2


x7 =

    8.0000
   14.0000
    2.3333


x8 =

    9.0000
   16.0000
    2.6667


x9 =

    10
    18
     3


x10 =

   11.0000
   20.0000
    3.3333

In the command window of Matlab®, we created a very basic for-end loop. For each step of the for-end loop, three new values will be calculated with the eval() command.

In this code here, we want to create 10 variables that will take the names x1, x2… up to x10. And all the new three values for each variable will take new values according to the functions inside the eval() command.

To obtain x1, x2… we typed ‘x’ inside quotes, and for coefficients, we used the num2str() command to type each step number which are the values of ‘a’ in each step, as strings as coefficients.

And inside quotes, we created our matrix in each element has a function created by the variable of ‘a’. So, for each value of ‘a’ for each loop, these three matrices will create three values for each x1, x2…

This is an another versatile use of eval() command in Matlab®.

So, [] inside eval() command, turns the strings inside it to operators.

This is the third and last example about eval() command in Matlab® command window to demonstrate the versatileness of eval() command.

>> x = [ 3 6 -6; 61 -8 -10; 13 5 -8];
for y = 1:3
    for z = 1:3
        if eval(['x(y,z)' '<=' '0'])
            x(y,z)=abs(x(y,z));
        end
    end
end
x

x =

     3     6     6
    61     8    10
    13     5     8

>> 

In the above example, we want to change all the negative values of matrix ‘x’ with the absolute values of them. We need to create a twofold for-end loop to question each element of matrix ‘x’.

The eval() command at the if-else query above, a question about whether the value of a current element of ‘x’ is smaller than zero. If it is true, the current element will be changed with the absolute value of this element.

We put the required elements inside quotes in square brackets to turn these elements into mathematical operators, inside eval() command.

Yes, this is an unnecessary use of eval() code. We can directly type the query with operators. But this example illustrates the usability of the eval() command at various kinds of duties.

Conclusion

Do not forget to leave your comments and questions about the use of the eval() command in Matlab® below. If you want further examples about the eval() command in Matlab®, please inform us in the comments.

Your precious feedbacks are very important for us.

Comments

2 responses to “The Use Of ‘eval()’ Command In Matlab® With Examples”

  1. momet Avatar
    momet

    Very helpful. Thank you 🙂

    1. MB-Editor Avatar
      MB-Editor

      you are welcome!

Leave a Reply

Your email address will not be published. Required fields are marked *


Please

leave feedback ?

( For the post )

ASK THE MECHANICALLAND

COMMUNITY


We are always open to your feedback to improve ourselves and the quality of our content! If you have any suggestions, thoughts, or criticism, please let us know. We are trying to improve our blog with constructive feedback. We are aware of how valuable your feedback is for our future development, and we will carefully read all your comments. Thank you in advance!