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
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.
> > 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
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
a hint:
- do NOT do this...
us