Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting a variable name to a string?

3,029 views
Skip to first unread message

Daniel

unread,
May 14, 2009, 7:27:01 AM5/14/09
to
In MATLAB, is there a simple way to convert the name of a variable to a string? I'm looking for something similar in principle to num2str and str2num.

So, if I had something like this:

variable1 = 1;
variable2 = 2;
X = input('Enter in either variable1 or variable2');
% so now X is assigned to 1 or 2, depending on user's choice
disp(<the word 'variable1' or 'variable2' as a string, depending on which one the user chooses>);

I want to use this basic concept in a program I am writing, admittely for no real purpose other than displaying a string that contains the actual name of a variable chosen by the user.

Not sure if I explained this very well, however I appreciate your help.

Daniel

Bruno Luong

unread,
May 14, 2009, 7:37:01 AM5/14/09
to
"Daniel " <simp...@hotmail.co.uk> wrote in message <gugv65$7ll$1...@fred.mathworks.com>...

> In MATLAB, is there a simple way to convert the name of a variable to a string? I'm looking for something similar in principle to num2str and str2num.
>
> So, if I had something like this:
>
> variable1 = 1;
> variable2 = 2;
> X = input('Enter in either variable1 or variable2');
> % so now X is assigned to 1 or 2, depending on user's choice
> disp(<the word 'variable1' or 'variable2' as a string, depending on which one the user chooses>);
>

vname=@(x) inputname(1);
toto=pi
s=vname(toto)

Bruno

Erkan Güzel

unread,
Feb 14, 2010, 10:11:05 AM2/14/10
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <gugvot$emb$1...@fred.mathworks.com>...

is there any way inverse of this?
i mean, i have a string and i wanna use it as a variable name.

Jan Simon

unread,
Feb 14, 2010, 10:35:24 AM2/14/10
to
Dear Erkan!

> > vname=@(x) inputname(1);
> > toto=pi
> > s=vname(toto)
> >
> > Bruno
>
> is there any way inverse of this?
> i mean, i have a string and i wanna use it as a variable name.

EVAL does evaluate expressions:
NameOfVariable = rand(1);
x = eval('NameOfVariable')

But be aware that code using the evil EVAL is hard to debug. In addition the result of the variable name '!format C:' may be unexpected.
A better idea is using structs with the dynamic field names feature:
S.VarName = 3;
String = 'VarName';
disp(S.(String));
String2 = 'asd'
S.(String2) = 5
disp(S)
Here '!format C:' is not dangerous...

Good luck, Jan

Steven Lord

unread,
Feb 15, 2010, 11:56:46 PM2/15/10
to

"Erkan Güzel" <erka...@gmail.com> wrote in message
news:hl93q9$gm0$1...@fred.mathworks.com...

> "Bruno Luong" <b.l...@fogale.findmycountry> wrote in message
> <gugvot$emb$1...@fred.mathworks.com>...
>> "Daniel " <simp...@hotmail.co.uk> wrote in message
>> <gugv65$7ll$1...@fred.mathworks.com>...
>> > In MATLAB, is there a simple way to convert the name of a variable to a
>> > string? I'm looking for something similar in principle to num2str and
>> > str2num.
>> >
>> > So, if I had something like this:
>> >
>> > variable1 = 1;
>> > variable2 = 2;
>> > X = input('Enter in either variable1 or variable2');
>> > % so now X is assigned to 1 or 2, depending on user's choice
>> > disp(<the word 'variable1' or 'variable2' as a string, depending on
>> > which one the user chooses>);

Call INPUT with the string 's' as the second input.

*snip*

> is there any way inverse of this?
> i mean, i have a string and i wanna use it as a variable name.

Yes, it is possible. However, you should not do this. See Q4.6 in the
newsgroup FAQ.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


us

unread,
Feb 16, 2010, 1:23:06 AM2/16/10
to

Brando Miranda

unread,
Feb 8, 2016, 5:55:11 PM2/8/16
to
Thanks for your suggestion bruno. But why do I have to create a function, why can't I just call a matlab function that already exists?

"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <gugvot$emb$1...@fred.mathworks.com>...

Bruno Luong

unread,
Feb 9, 2016, 1:46:09 AM2/9/16
to
"Brando Miranda" <bran...@mit.edu> wrote in message <n9b6c7$t7o$1...@newscl01ah.mathworks.com>...
> Thanks for your suggestion bruno. But why do I have to create a function, why can't I just call a matlab function that already exists?

Because such function doesn't exist?
0 new messages