Path: news.mathworks.com!not-for-mail
From: "Ashish Uthama" <first.last@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: genvarname doesn't work
Date: Fri, 10 Jul 2009 14:11:13 -0400
Organization: TMW
Lines: 39
Message-ID: <op.uwu54zbja5ziv5@uthamaa.dhcp.mathworks.com>
References: <h37t8i$6kh$1@fred.mathworks.com>
 <h37u9p$fho$1@fred.mathworks.com> <h37v27$6n4$1@fred.mathworks.com>
NNTP-Posting-Host: uthamaa.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1247249473 27260 172.31.57.126 (10 Jul 2009 18:11:13 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 10 Jul 2009 18:11:13 +0000 (UTC)
User-Agent: Opera Mail/9.63 (Win32)
Xref: news.mathworks.com comp.soft-sys.matlab:554542


On Fri, 10 Jul 2009 13:51:03 -0400, Juliette Salexa  
<juliette.physicist@gmail.com> wrote:

> Thank Wayne,
> I did clear('all') then reran the code and it worked, so it must have  
> been some predefined variable in my workspace messing it up.
>
> Anyway, I can't seem to get this to work.
>
> If I have an array of strings like:
>
> xhat=['a' 'b' 'c' 'd'];
>
> and I want to define 4 variables whose names are the elements of xhat..  
> let's say for example, I wanted a=1,b=2,c=3,d=4 ... it would be nice if  
> I could do:
>
> for i=1:4
>  genvarname(xhat(i))=i
> end
>
> But this does not work as I discovered in the documentation for  
> genvarname:
> "genvarname returns a string that can be used as a variable name. It  
> does not create a variable in the MATLAB workspace. You cannot,  
> therefore, assign a value to the output of genvarname."
>
> is there a way ??


 From the help:

     Examples:

         genvarname({'file','file'})     % returns {'file','file1'}
         a.(genvarname(' field#')) = 1   % returns a.field0x23 = 1

Using dynamic structure fields is probably the best approach if you want  
to do something like this.