Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: genvarname doesn't work
Date: Sun, 12 Jul 2009 22:11:42 -0400
Organization: The MathWorks, Inc.
Lines: 85
Message-ID: <h3e536$8q4$1@fred.mathworks.com>
References: <h37t8i$6kh$1@fred.mathworks.com> <op.uwu54zbja5ziv5@uthamaa.dhcp.mathworks.com> <h389nd$omc$1@fred.mathworks.com> <h38a6i$pud$1@fred.mathworks.com> <h38c66$5f9$1@fred.mathworks.com> <h38dor$hld$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1247451046 9028 144.212.105.187 (13 Jul 2009 02:10:46 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 13 Jul 2009 02:10:46 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
X-RFC2646: Format=Flowed; Original
Xref: news.mathworks.com comp.soft-sys.matlab:554854



"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message 
news:h38dor$hld$1@fred.mathworks.com...
>I read the FAQ page, thank you Steven and Oleg.
> It seems that Matlab is very adament about us not poofing variable names 
> at runtime.
>
> Is this only because it's harder to read, and therefore harder to debug ??
>
> Because they could change the syntax so that it's not so hard to read:
>
> x=['a' 'b' 'c' 'd'];
> for i=1:4
> Mygenvarname(x(i))=i
> end

Write this function:

function y = mysin(x)
eval('sin = 5');
y = sin(x)

and call it with:

y = mysin(1)

What do you expect this function call to return, and what does it actually 
return?

You may expect it to return the first element of the variable you created 
inside EVAL, 5.  It doesn't -- it returns the sine of 1 radian, because 
MATLAB parsed the M-file, found no indication it could see that sin was a 
variable, and so decided that the reference to sin on the third line was 
actually a call to the SIN function.

Now in a situation like this, with a three-line M-file, that problem is 
(relatively) easy to diagnose.  What if this was a three hundred-line 
function, and the EVAL call and the use of the "poofed" variable were 
separated by 100 lines of code?

In addition to bugs, if you create variables at runtime without any 
indication when the M-file is parsed that they will be variables, MATLAB may 
not be able to optimize it as effectively as it would if it knew at 
parse-time what was a variable and what wasn't.

> The reason why is that, for a very hypothetical example, say the letter n 
> represents the number of occurrences of 'n' in an article.
>
> If I want to report that number from an array, I would have to figure out 
> that n is the 14th letter of the alphabet, and type the command array(14) 
> to get my result.  What if I don't know off the top of my head that n is 
> 14th in the alphabet ?? I would rather be able to just type "n" and get my 
> answer.

convertLetterToIndex = @(x) x-'a'+1;
array(convertLetterToIndex('n'))

or

N = 14;
array(N)

or, if array is a struct array:

array.('n')

> Matlab's solution to this on the FAQ page is to use a structure s.a, a.b, 
> s.c .... s.n ....
>
> I'm not too familiar with structures .. but it seems as if they take up 
> more memory and are a more complex data structure in general .. it seems 
> like its harder to access or view information contained in them.. I'm 
> probably wrong, but can someone please explain to me why ??

Struct arrays do have some overhead above what is required for a regular 
array, and using them in code is somewhat different than using regular 
numeric arrays.  Yet if I had the choice to use struct arrays or EVAL in a 
piece of code to work with some sort of "dynamically created variables", I'd 
prefer structs over EVAL most of the time.

-- 
Steve Lord
slord@mathworks.com