Path: news.mathworks.com!not-for-mail
From: "John D'Errico" <woodchips@rochester.rr.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: eval, feval, global
Date: Thu, 27 Mar 2008 18:25:06 +0000 (UTC)
Organization: John D'Errico (1-3LEW5R)
Lines: 49
Message-ID: <fsgoq2$qcf$1@fred.mathworks.com>
References: <fsebf0$43u$1@fred.mathworks.com> <fsg56d$50c$1@fred.mathworks.com> <fsgmf0$fcd$1@fred.mathworks.com>
Reply-To: "John D'Errico" <woodchips@rochester.rr.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1206642306 27023 172.30.248.35 (27 Mar 2008 18:25:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 27 Mar 2008 18:25:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869215
Xref: news.mathworks.com comp.soft-sys.matlab:459524


"Yuri Geshelin" <geshelin@hotmail.com> wrote in message 
<fsgmf0$fcd$1@fred.mathworks.com>...
> All right, here is the second typical situation, in which I 
> use EVAL. It is not about computational cost, just a 
> programming habit.
> 
> 
> I have PLOT in the body of my function, and sometimes I 
> want to do
> 
> 
> plot(A.TEMP)
> 
> 
> at other times it would be another text string:
> 
> 
> plot(A.PSAL)
> 
> 
> At the time of the call to the function I do not know, 
> which field should be plotted.
> So I pass the array of text strings in the function (it 
> gets passed for other reasons, too):
> 
> 
> prop = {'TEMP', 'PSAL', ...........}
> 
> 
> And once I compute the index of the required field (irf), I 
> do this:
> 
> 
> eval(['plot(A.' prop{irf} ')'])
> 
> 
> Clumsy, but I got very much used to it. What would be your 
> recommendation?
> 
> 
> (I wanted to stay away from CASE, because the number of 
> fields is quite big).

Another sloppy programming habit, encouraged
by eval.

Use getfield, or better, dynamic field names.

John