converting variable into the a string
-
Hi, Can anyone tell me how to get the name of a variable as a string. Actually I want to store the values of the varibale( my varibale is a 2D array) in a text file named the same. Like if the name of the variable is myMatrix, then it should be able to store the values of the array in myMatrix.txt One more thing I want to do is that, I want to get user input as what should be given name of the variable. So, if user says,
>>a=[1,2,3;2,3,4];
My program should be able to create a matrix with variable name a and initiate it with the values given by the user. Regards Amit -
Hi, Can anyone tell me how to get the name of a variable as a string. Actually I want to store the values of the varibale( my varibale is a 2D array) in a text file named the same. Like if the name of the variable is myMatrix, then it should be able to store the values of the array in myMatrix.txt One more thing I want to do is that, I want to get user input as what should be given name of the variable. So, if user says,
>>a=[1,2,3;2,3,4];
My program should be able to create a matrix with variable name a and initiate it with the values given by the user. Regards AmitYou can't create variables dynamically, so there is no point of getting the name of a variable as a string. You already know the names of all your variables. What did you intend to use this for? I imagine that something like a collection would be more useful for you. --- b { font-weight: normal; }
-
You can't create variables dynamically, so there is no point of getting the name of a variable as a string. You already know the names of all your variables. What did you intend to use this for? I imagine that something like a collection would be more useful for you. --- b { font-weight: normal; }
thanks for your answer. but, i was wondering how MATLAB creates the variable name dynamically. if you enter something like the following
>> a=[1,2;2,3]
Then it creates a variable of name a and stores its value in its workspace, which you can later access it. Regards, Amit -
thanks for your answer. but, i was wondering how MATLAB creates the variable name dynamically. if you enter something like the following
>> a=[1,2;2,3]
Then it creates a variable of name a and stores its value in its workspace, which you can later access it. Regards, AmitMATLAB most likely created their own parser that can interpret their language. Depending on how far you wish to go you could do the same thing. You'll need to create a stack that contains all of the variables and there relevant values. If you want to just assign values to names you could create a Map of all the names to their values. You would then be able to access them like so: String myVal = (String)variables["a"]; This is a simplistic point of view on the whole matter of creating a parser though. Look up some articles on Abstract Syntax Trees (AST), and some parser generators such as Lex, Bison, SableCC, and ANTLR. Hope this helps. - Drew
-
thanks for your answer. but, i was wondering how MATLAB creates the variable name dynamically. if you enter something like the following
>> a=[1,2;2,3]
Then it creates a variable of name a and stores its value in its workspace, which you can later access it. Regards, AmitA variable in the MATLAB user environment does not exist as a variable in the MATLAB application itself. Both the name and the contents of the user variable is data to the application. The variable names and the data is stored in a collection or something similar. If you want the same functionality, use a collection. --- b { font-weight: normal; }
-
Hi, Can anyone tell me how to get the name of a variable as a string. Actually I want to store the values of the varibale( my varibale is a 2D array) in a text file named the same. Like if the name of the variable is myMatrix, then it should be able to store the values of the array in myMatrix.txt One more thing I want to do is that, I want to get user input as what should be given name of the variable. So, if user says,
>>a=[1,2,3;2,3,4];
My program should be able to create a matrix with variable name a and initiate it with the values given by the user. Regards Amityou could probably model your user variable like this
public class UserVariable { private string variableName; private object variableContent; public string VariableName { get { return variableName; } set { variableName = value; } } public string VariableContent { get { return variableContent; } set { variableContent = value; } } public UserVariable(string name,object content) { this.variableName = name; this.variableContent = content; } // automatically write the content of the variable when used in // a string.Format public override string ToString() { return string.Format("{0}",variableContent); } }
this could be the head of a series of specialised Variable.. just my 2 cents Denevers