Prev: Fuzzy inference system
Next: cell array in uitable
From: Erkan Güzel on 14 Feb 2010 10:11 "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <gugvot$emb$1(a)fred.mathworks.com>... > "Daniel " <simpson.d(a)hotmail.co.uk> wrote in message <gugv65$7ll$1(a)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 is there any way inverse of this? i mean, i have a string and i wanna use it as a variable name.
From: Jan Simon on 14 Feb 2010 10:35 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
From: Steven Lord on 15 Feb 2010 23:56 "Erkan Güzel" <erkan.ehm(a)gmail.com> wrote in message news:hl93q9$gm0$1(a)fred.mathworks.com... > "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message > <gugvot$emb$1(a)fred.mathworks.com>... >> "Daniel " <simpson.d(a)hotmail.co.uk> wrote in message >> <gugv65$7ll$1(a)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 slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: us on 16 Feb 2010 01:23 "Daniel " <simpson.d(a)hotmail.co.uk> wrote in message <gugv65$7ll$1(a)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>); > > 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 a hint: - do NOT do this... http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F us
|
Pages: 1 Prev: Fuzzy inference system Next: cell array in uitable |