Thread Subject: changing a name of a variable form some stored string

Subject: changing a name of a variable form some stored string

From: rohit

Date: 6 Jun, 2009 19:37:01

Message: 1 of 10

 I have a problem in writing algorithm or getting the function to solve the problem like this:
a='y';
b=9;
 I want to change the name of the variable 'b' by 'y'.
please someone help me to solve this problem........

Subject: changing a name of a variable form some stored string

From: Jos

Date: 6 Jun, 2009 19:47:01

Message: 2 of 10

"rohit " <sci_vip@yahoo.co.in> wrote in message <h0eggt$6ff$1@fred.mathworks.com>...
> I have a problem in writing algorithm or getting the function to solve the problem like this:
> a='y';
> b=9;
> I want to change the name of the variable 'b' by 'y'.
> please someone help me to solve this problem........

I know the answer, but I won't tell you ...

First ask yourselve why you need to code it like you describe. A better, more versatile approach which is less prone to errors would be to use structs:

a = 'y'
b = 2
MyStruct.(a) = b

hth
Jos

Subject: changing a name of a variable form some stored string

From: Travis

Date: 12 Jul, 2009 00:16:03

Message: 3 of 10

"Jos " <#10584@fileexchange.com> wrote in message <h0eh3l$dvg$1@fred.mathworks.com>...
> "rohit " <sci_vip@yahoo.co.in> wrote in message <h0eggt$6ff$1@fred.mathworks.com>...
> > I have a problem in writing algorithm or getting the function to solve the problem like this:
> > a='y';
> > b=9;
> > I want to change the name of the variable 'b' by 'y'.
> > please someone help me to solve this problem........
>
> I know the answer, but I won't tell you ...
>
> First ask yourselve why you need to code it like you describe. A better, more versatile approach which is less prone to errors would be to use structs:
>
> a = 'y'
> b = 2
> MyStruct.(a) = b
>
> hth
> Jos

I would also like to know how to do this. I can successfully use the structure suggestion posted, but I would really like to have the data as a stand alone variable (easier to pass along and reference later in my gui's).

Subject: changing a name of a variable form some stored string

From: Bruno Luong

Date: 12 Jul, 2009 05:38:01

Message: 4 of 10

"Travis " <sinusoid2@hotmail.com> wrote in message <h3ba03$pse$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <h0eh3l$dvg$1@fred.mathworks.com>...
> > "rohit " <sci_vip@yahoo.co.in> wrote in message <h0eggt$6ff$1@fred.mathworks.com>...
> > > I have a problem in writing algorithm or getting the function to solve the problem like this:
> > > a='y';
> > > b=9;
> > > I want to change the name of the variable 'b' by 'y'.
> > > please someone help me to solve this problem........
> >
> > I know the answer, but I won't tell you ...
> >
> > First ask yourselve why you need to code it like you describe. A better, more versatile approach which is less prone to errors would be to use structs:
> >
> > a = 'y'
> > b = 2
> > MyStruct.(a) = b
> >
> > hth
> > Jos
>
> I would also like to know how to do this. I can successfully use the structure suggestion posted, but I would really like to have the data as a stand alone variable (easier to pass along and reference later in my gui's).

% First method
eval([a '= b;']);

% Second method using WORKSPACE http://www.mathworks.com/matlabcentral/fileexchange/23078

ws = workspace; % FEX, called only once
a='y';
b=pi;
ws.(a)=b;
y

% Bruno

Subject: changing a name of a variable form some stored string

From: Travis

Date: 12 Jul, 2009 06:49:02

Message: 5 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h3bsrp$6vs$1@fred.mathworks.com>...
> "Travis " <sinusoid2@hotmail.com> wrote in message <h3ba03$pse$1@fred.mathworks.com>...
> > "Jos " <#10584@fileexchange.com> wrote in message <h0eh3l$dvg$1@fred.mathworks.com>...
> > > "rohit " <sci_vip@yahoo.co.in> wrote in message <h0eggt$6ff$1@fred.mathworks.com>...
> > > > I have a problem in writing algorithm or getting the function to solve the problem like this:
> > > > a='y';
> > > > b=9;
> > > > I want to change the name of the variable 'b' by 'y'.
> > > > please someone help me to solve this problem........
> > >
> > > I know the answer, but I won't tell you ...
> > >
> > > First ask yourselve why you need to code it like you describe. A better, more versatile approach which is less prone to errors would be to use structs:
> > >
> > > a = 'y'
> > > b = 2
> > > MyStruct.(a) = b
> > >
> > > hth
> > > Jos
> >
> > I would also like to know how to do this. I can successfully use the structure suggestion posted, but I would really like to have the data as a stand alone variable (easier to pass along and reference later in my gui's).
>
> % First method
> eval([a '= b;']);
>
> % Second method using WORKSPACE http://www.mathworks.com/matlabcentral/fileexchange/23078
>
> ws = workspace; % FEX, called only once
> a='y';
> b=pi;
> ws.(a)=b;
> y
>
> % Bruno

Thank you Bruno, as always you are a hige help. One more question. How would I go about making this new variable global within code? I am starting to think just attaching it to a pre-existing struct, which is global, might be easier and cleaner.

Subject: changing a name of a variable form some stored string

From: Travis

Date: 12 Jul, 2009 06:49:03

Message: 6 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h3bsrp$6vs$1@fred.mathworks.com>...
> "Travis " <sinusoid2@hotmail.com> wrote in message <h3ba03$pse$1@fred.mathworks.com>...
> > "Jos " <#10584@fileexchange.com> wrote in message <h0eh3l$dvg$1@fred.mathworks.com>...
> > > "rohit " <sci_vip@yahoo.co.in> wrote in message <h0eggt$6ff$1@fred.mathworks.com>...
> > > > I have a problem in writing algorithm or getting the function to solve the problem like this:
> > > > a='y';
> > > > b=9;
> > > > I want to change the name of the variable 'b' by 'y'.
> > > > please someone help me to solve this problem........
> > >
> > > I know the answer, but I won't tell you ...
> > >
> > > First ask yourselve why you need to code it like you describe. A better, more versatile approach which is less prone to errors would be to use structs:
> > >
> > > a = 'y'
> > > b = 2
> > > MyStruct.(a) = b
> > >
> > > hth
> > > Jos
> >
> > I would also like to know how to do this. I can successfully use the structure suggestion posted, but I would really like to have the data as a stand alone variable (easier to pass along and reference later in my gui's).
>
> % First method
> eval([a '= b;']);
>
> % Second method using WORKSPACE http://www.mathworks.com/matlabcentral/fileexchange/23078
>
> ws = workspace; % FEX, called only once
> a='y';
> b=pi;
> ws.(a)=b;
> y
>
> % Bruno

Thank you Bruno, as always you are a hige help. One more question. How would I go about making this new variable global within code? I am starting to think just attaching it to a pre-existing struct, which is global, might be easier and cleaner.

Subject: changing a name of a variable form some stored string

From: Bruno Luong

Date: 12 Jul, 2009 07:24:01

Message: 7 of 10

"Travis " <sinusoid2@hotmail.com> wrote in message <h3c10v$snf$1@fred.mathworks.com>...

>
> Thank you Bruno, as always you are a hige help. One more question. How would I go about making this new variable global within code? I am starting to think just attaching it to a pre-existing struct, which is global, might be easier and cleaner.

You can still use EVAL with GLOBAL. But EVAL alone is EVIL enough. If you put EVAL together GLOBAL, that would be EVIL power two or a true global evil. LOL. Yes put it in struct is better.

Bruno

Subject: changing a name of a variable form some stored string

From: Travis

Date: 12 Jul, 2009 14:33:01

Message: 8 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h3c32h$d19$1@fred.mathworks.com>...
> "Travis " <sinusoid2@hotmail.com> wrote in message <h3c10v$snf$1@fred.mathworks.com>...
>
> >
> > Thank you Bruno, as always you are a hige help. One more question. How would I go about making this new variable global within code? I am starting to think just attaching it to a pre-existing struct, which is global, might be easier and cleaner.
>
> You can still use EVAL with GLOBAL. But EVAL alone is EVIL enough. If you put EVAL together GLOBAL, that would be EVIL power two or a true global evil. LOL. Yes put it in struct is better.
>
> Bruno

haha, thanks Bruno, I will stick with the structures.

Subject: changing a name of a variable form some stored string

From: Steven Lord

Date: 13 Jul, 2009 00:43:51

Message: 9 of 10


"Travis " <sinusoid2@hotmail.com> wrote in message
news:h3ba03$pse$1@fred.mathworks.com...
> "Jos " <#10584@fileexchange.com> wrote in message
> <h0eh3l$dvg$1@fred.mathworks.com>...
>> "rohit " <sci_vip@yahoo.co.in> wrote in message
>> <h0eggt$6ff$1@fred.mathworks.com>...
>> > I have a problem in writing algorithm or getting the function to solve
>> > the problem like this:
>> > a='y';
>> > b=9;
>> > I want to change the name of the variable 'b' by 'y'.
>> > please someone help me to solve this problem........
>>
>> I know the answer, but I won't tell you ...
>>
>> First ask yourselve why you need to code it like you describe. A better,
>> more versatile approach which is less prone to errors would be to use
>> structs:
>>
>> a = 'y'
>> b = 2
>> MyStruct.(a) = b
>>
>> hth
>> Jos
>
> I would also like to know how to do this. I can successfully use the
> structure suggestion posted, but I would really like to have the data as a
> stand alone variable (easier to pass along and reference later in my
> gui's).

I strongly encourage you NOT to create variables like this at runtime. Read
Q4.6 in the FAQ:

http://matlabwiki.mathworks.com/MATLAB_FAQ

--
Steve Lord
slord@mathworks.com

Subject: changing a name of a variable form some stored string

From: Travis

Date: 13 Jul, 2009 01:48:00

Message: 10 of 10

"Steven Lord" <slord@mathworks.com> wrote in message <h3dvuf$eiu$1@fred.mathworks.com>...
>
> "Travis " <sinusoid2@hotmail.com> wrote in message
> news:h3ba03$pse$1@fred.mathworks.com...
> > "Jos " <#10584@fileexchange.com> wrote in message
> > <h0eh3l$dvg$1@fred.mathworks.com>...
> >> "rohit " <sci_vip@yahoo.co.in> wrote in message
> >> <h0eggt$6ff$1@fred.mathworks.com>...
> >> > I have a problem in writing algorithm or getting the function to solve
> >> > the problem like this:
> >> > a='y';
> >> > b=9;
> >> > I want to change the name of the variable 'b' by 'y'.
> >> > please someone help me to solve this problem........
> >>
> >> I know the answer, but I won't tell you ...
> >>
> >> First ask yourselve why you need to code it like you describe. A better,
> >> more versatile approach which is less prone to errors would be to use
> >> structs:
> >>
> >> a = 'y'
> >> b = 2
> >> MyStruct.(a) = b
> >>
> >> hth
> >> Jos
> >
> > I would also like to know how to do this. I can successfully use the
> > structure suggestion posted, but I would really like to have the data as a
> > stand alone variable (easier to pass along and reference later in my
> > gui's).
>
> I strongly encourage you NOT to create variables like this at runtime. Read
> Q4.6 in the FAQ:
>
> http://matlabwiki.mathworks.com/MATLAB_FAQ
>
> --
> Steve Lord
> slord@mathworks.com
>

I found that using the preexisting structure works quite well, I had in my mind that a stand alone variable would be better, but it looks like passing it along will create numerous other issues and complications that the structure will not.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
evil eval Jos (10584) 6 Jun, 2009 15:49:04
structures Jos (10584) 6 Jun, 2009 15:49:04
rssFeed for this Thread

Contact us at files@mathworks.com