From: Delia on 4 Jun 2010 19:25 Hi, I want to use the name of a variable as a string (for example as xlabel of a plot). How do I do that?. I kind of what to achieve this: x = [1 2 3] plot(x) xlabel('x') without using the explicit name of the variable in the last line (assume I loop over many variables and I do not the name). Thank you for the help delia
From: Walter Roberson on 4 Jun 2010 23:36 Delia wrote: > Hi, I want to use the name of a variable as a string (for example as > xlabel of a plot). How do I do that?. I kind of what to achieve this: > x = [1 2 3] > plot(x) > xlabel('x') > without using the explicit name of the variable in the last line (assume > I loop over many variables and I do not the name). In order to assume that you are looping over variables, I have to assume that you have the name of the variable somehow, in which case you do not have any difficulty in using the name as a string. The only way I can see at the moment that might throw things off is if you are looping over components of a symbolic expression, and you have a symbol whose _value_ is the name of another symbol. I do not have the symbolic toolbox to test out that case, and it is different enough from your example that it seems unlikely. How _exactly_ are you looping over variables without having the name of a variable in a string already? If you are making a function call and passing in eval() of the name of a variable, and you want to recover that name, then _possibly_ inputname() might do the trick for you, but I would not count on it, as I would expect that Matlab would consider eval() of something to be an anonymous expression that had no name. I get the impression that what you are probably really after is something that you should be avoiding. See http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
From: Rajiv Narayan on 4 Jun 2010 23:53 "Delia " <deliatosi(a)hotmail.com.remove.this> wrote in message <huc210$kc9$1(a)fred.mathworks.com>... > Hi, I want to use the name of a variable as a string (for example as xlabel of a plot). How do I do that?. > I kind of what to achieve this: > x = [1 2 3] > plot(x) > xlabel('x') > without using the explicit name of the variable in the last line (assume I loop over many variables and I do not the name). > Thank you for the help > delia Its probably best if you use arrays or cells, instead of having separate variables. If your variables are named in a deterministic fashion, you could try something like: d1 = rand(10,1); d2 = rand(10,1); v = who('d*'); for ii=1:length(v) figure plot(eval(v{ii})); xlabel(v{ii}); end
From: sscnekro on 5 Jun 2010 06:20 > Hi, I want to use the name of a variable as a string (for example as xlabel of a plot). > without using the explicit name of the variable in the last line (assume I loop over many variables and I do not the name). Hi, I'm not sure I understand what you want to do. In the event, you just incline to naming variables in a loop, bcs you are used to do it from other languages, pls note that in ML evth includes a reference in itself. It took me quite a moment to see it. So if you store your whatever stuff in a cell array or in a matrix, you simply can refer to elements or groups of elements in there (using a single or more dimension index). x = {1.683434, 2.973835, 3.803803} for ii = 1:3 thelabel{ii} = num2str(x{ii}) .... xlabel(thelabel{ii}) end Otherwise, do you just need to get all values of x on the x axis in a string format?
|
Pages: 1 Prev: sparse matrix creation Next: Figures don't preserve size on export |