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

2 min reading time..

In MatLab, eval() is very versatile command that you can easily use in lots of purposes. In here, we show you how to use eval() command in various examples done in MatLab command window.

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

How To Use ‘eval()’ Command In MatLab?

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 above example, you can use eval() command bu typingfunctions 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 function inside it.

This is an another example about the use of eval() command in 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 command window of Matlab, we created a very basic for-end loop. For each step of for-end loop, three new values will be calculated with eval() command.

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

To obtain x1, x2… we typed ‘x’ inside quotes, and for coefficients, we used num2str() command to type the 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 elements has a function created by the variable of ‘a’. So, for each values of ‘a’ for each loop, these three matrices will create three values for each x1, x2…

Download And Install MatLab 2019!

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

>> 

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

The eval() command at if-else query above, question about whether the value of current element of ‘x’ smaller than zero. If it is true, the current element will be changed with 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 unnecesary use of eval() code. We can directly type the query with operators. But this example illustrates the usability of eval() command at various kinds of duties.

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

Your precious feedbacks are very important for us.

COMMENTS
There are no comments for this article yet! You can make the first comment.
Leave a Comment

x