Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Using EVAL.

Subject: Using EVAL.

From: Husam Aldahiyat

Date: 31 Oct, 2008 09:15:04

Message: 1 of 6

Hello,
 I have recently started toying around with the eval function but I still don't know all its disadvantages, uses, why it's there, etc. So if it wouldn't be too much a hassle could someone put me "in the know" and tell me where I should use it and why??? Thanks!

Subject: Using EVAL.

From: Steve Amphlett

Date: 31 Oct, 2008 09:39:01

Message: 2 of 6

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <geeiao$ii0$1@fred.mathworks.com>...
> Hello,
> I have recently started toying around with the eval function but I still don't know all its disadvantages, uses, why it's there, etc. So if it wouldn't be too much a hassle could someone put me "in the know" and tell me where I should use it and why??? Thanks!

=====

Use it ONLY when all else has failed. Create a string with your command:

cmd='something clever' % Note, no semilcolon, so you can see the command.

then eval it:

eval(cmd)

Doing this all in one line will only end in either tears or frustration.

p.s. It's unlikely that you need to use eval.

Subject: Using EVAL.

From: Husam Aldahiyat

Date: 31 Oct, 2008 09:48:02

Message: 3 of 6

Why? I know how to use it and have done so while creating cool pieces of code. The problem is that it seems "too easy" or too good to be true.

Subject: Using EVAL.

From: John D'Errico

Date: 31 Oct, 2008 10:15:03

Message: 4 of 6

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <geek8i$8am$1@fred.mathworks.com>...
> Why? I know how to use it and have done so while creating cool pieces of code. The problem is that it seems "too easy" or too good to be true.

The problem is, you know how to MISUSE it.

Use of eval is one thing that is likely to create
difficult to find bugs in your code.

Eval is SLOW, at least relatively so. Code that
uses eval cannot be optimized by the parser
for efficiency.

The uses for eval that novices seem to find
are almost all things that can be done better,
faster, more easily, etc., using the existing
utilities in matlab. But, eval is the lazy way
out. Why learn to use the language as it can
be used when eval is there?

A good example of the evil eval is the user
who creates thousands of variables, named
something like x1, x2, x3, x4, x5, ...

Yes, this can be done using eval. Yes, it is
insane, since either a cell array, a structure,
or perhaps a higher dimensional array are all
better options, depending on the problem.

So, avoid eval, unless there is absolutely no
other option. And even then, look an extra
time for the other options. Those other
options are there, and they are better choices
than eval.

John

Subject: Using EVAL.

From: Loren Shure

Date: 31 Oct, 2008 14:02:06

Message: 5 of 6

In article <geeiao$ii0$1@fred.mathworks.com>, numandina@gmail.com
says...
> Hello,
> I have recently started toying around with the eval function but I still don't know all its disadvantages, uses, why it's there, etc. So if it wouldn't be too much a hassle could someone put me "in the know" and tell me where I should use it and why??? Thanks!
>

I recommend this article:

 http://blogs.mathworks.com/loren/2005/12/28/evading-eval/

--
Loren
http://blogs.mathworks.com/loren

Subject: Using EVAL.

From: Husam Aldahiyat

Date: 31 Oct, 2008 14:34:02

Message: 6 of 6

Thanks for the answers everyone. I've been using MATLAB for about a year and in that time I've always found trouble here and there, but none that I wouldn't figure out sooner or later. I'll try to evade EVAL unless I want to show off to fellow students who have never heard of it, hehe ;)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us