Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Why does everyone hate 'eval'?
Date: Thu, 11 Dec 2008 20:53:03 +0000 (UTC)
Organization: Xoran Technologies
Lines: 32
Message-ID: <ghrujf$d4d$1@fred.mathworks.com>
References: <ghrlim$oqo$1@fred.mathworks.com> <ghrsc8$194$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1229028783 13453 172.30.248.37 (11 Dec 2008 20:53:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 11 Dec 2008 20:53:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:506427


"Matt" <mjacobson.removethis@xorantech.com> wrote in message <ghrsc8$194$1@fred.mathworks.com>...
> 
> The one time that I have found eval() indispensible is in creating overloaded subsref/subsasgn methods for classes.
> 
> Calling subsref()/subsasgn() by themselves is known to produce unpredictable behavior because of dispatching rules, but if you execute the equivalent expression using eval this tends not to happen.
> 
> 

For example, if I tried to evaluate this,

A{i}=SomeObject;

using subsasgn() as follows

S=substruct('{}',{i});
A=subsasgn(A,S,SomeObject); 

then it may produce non-equivalent behavior because in the  2nd instance the subsasgn() method of  class(SomeObject) will be called. 
It takes precedence over the built-in subsasgn methods for @cell types.

However, what does one do if S is in some way dynmically varying?

Maybe I'm running assignments to A in a loop and in one instance S is as above but in later iterations of the loop I have any or both of the following

S=substruct('{}',{i},'{}',{j}) 
S=substruct('{}',{i,j}) 

The only solution I've ever been able to find is to derive an indexing expression for each such S in string form and execute it using eval().

If I'm missing a basic alternative though, I welcome advice...