# Creating "sequential variable names"

14 messages
Open this post in threaded view
|

## Creating "sequential variable names"

 Hello world, I've got a problem in defyining "sequential name variable" . I would like to create name variables such as b1, b2, b100 and to assign them values, e.g. b1=1, b2=2, ... , b100 = 100. I would obviously not to assign every variable at once, I'd like to use a for loop similar to for i = 1:100 bi=i; endfor This is not the right answer, but I don't know how to create sequentially name variables like b1, b2, etc. I could create a string name strcat("b",nu2str(i)) , but I don't solve my problem. I got the same problem using b_i = i, does anyone have an idea? Thanks everybody, have a nice day,      Alberto
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 On Mon, Oct 18, 2010 at 2:21 PM, AlbFrigerio <[hidden email]> wrote: > > Hello world, I've got a problem in defyining "sequential name variable" . I > would like to create name variables such as b1, b2, b100 and to assign them > values, e.g. b1=1, b2=2, ... , b100 = 100. I would obviously not to assign > every variable at once, I'd like to use a for loop similar to > > for i = 1:100 > bi=i; > endfor > > This is not the right answer, but I don't know how to create sequentially > name variables like b1, b2, etc. I could create a string name > strcat("b",nu2str(i)) , but I don't solve my problem. I got the same problem > using b_i = i, does anyone have an idea? > > Thanks everybody, have a nice day, >     Alberto (I am wondering why you don't use an array.) for i = 1:10         evalstr=sprintf("b%i=%i;",i,i);         printf("%s\n",evalstr);         eval(evalstr); endfor -- /* andy buckle */ _______________________________________________ Help-octave mailing list [hidden email] https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 In reply to this post by AlbFrigerio Hi man, 18 10 2010 kl. 06:21 -0700, skrev AlbFrigerio: > Hello world, I've got a problem in defyining "sequential name variable" . I > would like to create name variables such as b1, b2, b100 and to assign them > values, e.g. b1=1, b2=2, ... , b100 = 100. I would obviously not to assign > every variable at once, I'd like to use a for loop similar to > > for i = 1:100 > bi=i; > endfor > > This is not the right answer, but I don't know how to create sequentially > name variables like b1, b2, etc. I could create a string name > strcat("b",nu2str(i)) , but I don't solve my problem. I got the same problem > using b_i = i, does anyone have an idea? You can this using the 'eval' function, i.e.   for i = 1:100     eval (sprintf ("b%d = %d;", i, i));   endfor but are you really sure that's what you want? Why not just create an array with the values you need? That is,   b = 1:100; and then index that array 'b (i)' when you need the value. Søren _______________________________________________ Help-octave mailing list [hidden email] https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 Thank you so much Andy and Søren, your help was very fast and I got what I wanted to. Søren, you are completely right, but I'm not (yet) so crazy to create 100 variables instead of using a vector :) . I just proposed an example, I just needed to know ho to create sequential names. Thanks again,   Alberto Søren Hauberg wrote Hi man, 18 10 2010 kl. 06:21 -0700, skrev AlbFrigerio: > Hello world, I've got a problem in defyining "sequential name variable" . I > would like to create name variables such as b1, b2, b100 and to assign them > values, e.g. b1=1, b2=2, ... , b100 = 100. I would obviously not to assign > every variable at once, I'd like to use a for loop similar to > > for i = 1:100 > bi=i; > endfor > > This is not the right answer, but I don't know how to create sequentially > name variables like b1, b2, etc. I could create a string name > strcat("b",nu2str(i)) , but I don't solve my problem. I got the same problem > using b_i = i, does anyone have an idea? You can this using the 'eval' function, i.e.   for i = 1:100     eval (sprintf ("b%d = %d;", i, i));   endfor but are you really sure that's what you want? Why not just create an array with the values you need? That is,   b = 1:100; and then index that array 'b (i)' when you need the value. Søren _______________________________________________ Help-octave mailing list Help-octave@octave.org https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 Administrator On Oct 18, 2010, at 9:55 PM, AlbFrigerio wrote: > Thank you so much Andy and Søren, your help was very fast and I got what I > wanted to. > > Søren, you are completely right, but I'm not (yet) so crazy to create 100 > variables instead of using a vector :) . I just proposed an example, I just > needed to know ho to create sequential names. > > Thanks again, >  Alberto Its not clear what you'd like to do, but ... If you have a sequence of names stored in a cell array ...         names = {"name1", "name2", "name3" ...}; You can store values associated with each name as a structure. For example ...         s.(names{n}) = values{n) Ben _______________________________________________ Help-octave mailing list [hidden email] https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 In reply to this post by AlbFrigerio man, 18 10 2010 kl. 06:55 -0700, skrev AlbFrigerio: > Søren, you are completely right, but I'm not (yet) so crazy to create 100 > variables instead of using a vector :) . I just proposed an example, I just > needed to know ho to create sequential names. If you need to store something more complicated than numbers in the variables, you should consider using a cell array. Something like   b = cell (3, 1);   b {1} = "This corresponds to b_1";   b {2} = complex (3, 1);   b {3} = "And this corresponds to b_3"; and so forth. I once (many years ago) had to maintain some software another guy wrote that used sequential variable naming and it was a major pain. For your own mental wellness I would really consider another solution than using b1, b2, ... :-) Cheers Søren _______________________________________________ Help-octave mailing list [hidden email] https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 Dear Søren, I'll try in my awful English to explain why I decided to use sequential names. I got ten variables, I want to call AG1, ... , AG10. Every AG const of several elements : P,A,M,alpha, beta, gamma. Some of the previous elements const again of other elements : n, err, fitness, etc As you suggested I tried to use struct : AG1 is a struct whose elements are P,M, etc. P is again a struct whose elements are n, err, etc. I believe there is a better way to implement it, but in this environment I don't want to type AG1.P.n = 1 ; AG2.P.n = 1, ... AG10.P.n =1 , AG1.M.err = 0, ... AG10.M.err = 0 , it is faster (and easier reading) what you suggested : for i = 1 : 10     eval(sprintf("AG%d.P.n=1;AG%d.M.err=0",i,i)); endfor I hope you understood what is my need, as I told you I believe there is a nicer way to solve it, but I'm only a rookie in Octave :) By the way, I got another question to you. Sometimes in the sprintf command I may use a reference to a function. Everything works fine, but if a put a keyboard command in the function (to start debug mode) my Octave crashes : it doesn't let me use the command line and after ctrl+C it crashes . Have you got any idea?Thanks again, have a nice evening,    Alberto Søren Hauberg wrote man, 18 10 2010 kl. 06:55 -0700, skrev AlbFrigerio: > Søren, you are completely right, but I'm not (yet) so crazy to create 100 > variables instead of using a vector :) . I just proposed an example, I just > needed to know ho to create sequential names. If you need to store something more complicated than numbers in the variables, you should consider using a cell array. Something like   b = cell (3, 1);   b {1} = "This corresponds to b_1";   b {2} = complex (3, 1);   b {3} = "And this corresponds to b_3"; and so forth. I once (many years ago) had to maintain some software another guy wrote that used sequential variable naming and it was a major pain. For your own mental wellness I would really consider another solution than using b1, b2, ... :-) Cheers Søren _______________________________________________ Help-octave mailing list Help-octave@octave.org https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 Administrator On 18-Oct-2010, AlbFrigerio wrote: | | Dear Søren, I'll try in my awful English to explain why I decided to use | sequential names. | | I got ten variables, I want to call AG1, ... , AG10. | Every AG const of several elements : P,A,M,alpha, beta, gamma. | Some of the previous elements const again of other elements : n, err, | fitness, etc | | As you suggested I tried to use struct : AG1 is a struct whose elements are | P,M, etc. P is again a struct whose elements are n, err, etc. I believe | there is a better way to implement it, but in this environment I don't want | to type AG1.P.n = 1 ; AG2.P.n = 1, ... AG10.P.n =1 , AG1.M.err = 0, ... | AG10.M.err = 0 , it is faster (and easier reading) what you suggested : | | for i = 1 : 10 |     eval(sprintf("AG%d.P.n=1;AG%d.M.err=0",i,i)); | endfor I think it is even clearer if you write   for i = 1:10     AG(i).P.n = 1;     AG(i).M.err = 0;   endfor to create a struct array. If you find yourself using eval for what seem should be normal programming tasks, then you should immediately think there must be a better way, and nearly always there is. | I hope you understood what is my need, as I told you I believe there is a | nicer way to solve it, but I'm only a rookie in Octave :) | | By the way, I got another question to you. Sometimes in the sprintf command | I may use a reference to a function. Everything works fine, but if a put a | keyboard command in the function (to start debug mode) my Octave crashes : | it doesn't let me use the command line and after ctrl+C it crashes . Have | you got any idea? If you think you've found a bug in Octave, please submit a complete bug report to the bug tracker here:   https://savannah.gnu.org/bugs/?func=additem&group=octaveWhen reporting a bug, you need to provide everything necessary for someone else to reproduce the problem.  Please read http://www.octave.org/bugs.html for some tips about how to make a useful bug report. jwe _______________________________________________ Help-octave mailing list [hidden email] https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 --- On Mon, 10/18/10, John W. Eaton <[hidden email]> wrote: > From: John W. Eaton <[hidden email]> > Subject: Re: Creating "sequential variable names" > To: "AlbFrigerio" <[hidden email]> > Cc: [hidden email] > Date: Monday, October 18, 2010, 8:48 AM > On 18-Oct-2010, AlbFrigerio wrote: > [snip] > If you find yourself using eval for what seem should be > normal > programming tasks, then you should immediately think there > must be a > better way, and nearly always there is. [snip] > jwe > I had difficulties using 'plot'. I.e. I needed a variable number 'X, Y, FORMAT' triplets, and I was generating a string representing the 'plot' command to be ultimately executed, then I used 'eval' to execute the command. The triplets were generated in a loop and the string was filled in teh same loop. So, which is a better way and is it described in the documentation ? I am still using octave-3.0.5 and reading 'help plot' I didn't find a way without 'eval'. Thanks,   Sergei.       _______________________________________________ Help-octave mailing list [hidden email] https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 Administrator On 18-Oct-2010, Sergei Steshenko wrote: | | | --- On Mon, 10/18/10, John W. Eaton <[hidden email]> wrote: | | > From: John W. Eaton <[hidden email]> | > Subject: Re: Creating "sequential variable names" | > To: "AlbFrigerio" <[hidden email]> | > Cc: [hidden email] | > Date: Monday, October 18, 2010, 8:48 AM | > On 18-Oct-2010, AlbFrigerio wrote: | > | [snip] | > If you find yourself using eval for what seem should be | > normal | > programming tasks, then you should immediately think there | > must be a | > better way, and nearly always there is. | [snip] | > jwe | > | | I had difficulties using 'plot'. I.e. I needed a variable number 'X, Y, FORMAT' triplets, and I was generating a string representing the 'plot' | command to be ultimately executed, then I used 'eval' to execute the | command. | | The triplets were generated in a loop and the string was filled in teh same loop. | | So, which is a better way and is it described in the documentation ? | I am still using octave-3.0.5 and reading 'help plot' I didn't find a way | without 'eval'. If I understand correctly what you are trying to do, then something like the following should work:   args = cell (nargs, 1);   args{1} = x1;   args{2} = y1;   args{3} = fmt1;   args{4} = x2;   args{5} = y2;   args{6} = fmt3;   ...   plot (args{:}); jwe _______________________________________________ Help-octave mailing list [hidden email] https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 In reply to this post by John W. Eaton Thanks a lot jwe, it looks very simple, as all the right things should be. I implemented it and everything works without using eval or something like that. About the (possible) bug problem, I'll try in these days to understand what went wrong, if I don't reach any solution I'll report it to the bug section. Thanks again,    Alberto John W. Eaton wrote On 18-Oct-2010, AlbFrigerio wrote: | | Dear Søren, I'll try in my awful English to explain why I decided to use | sequential names. | | I got ten variables, I want to call AG1, ... , AG10. | Every AG const of several elements : P,A,M,alpha, beta, gamma. | Some of the previous elements const again of other elements : n, err, | fitness, etc | | As you suggested I tried to use struct : AG1 is a struct whose elements are | P,M, etc. P is again a struct whose elements are n, err, etc. I believe | there is a better way to implement it, but in this environment I don't want | to type AG1.P.n = 1 ; AG2.P.n = 1, ... AG10.P.n =1 , AG1.M.err = 0, ... | AG10.M.err = 0 , it is faster (and easier reading) what you suggested : | | for i = 1 : 10 |     eval(sprintf("AG%d.P.n=1;AG%d.M.err=0",i,i)); | endfor I think it is even clearer if you write   for i = 1:10     AG(i).P.n = 1;     AG(i).M.err = 0;   endfor to create a struct array. If you find yourself using eval for what seem should be normal programming tasks, then you should immediately think there must be a better way, and nearly always there is. | I hope you understood what is my need, as I told you I believe there is a | nicer way to solve it, but I'm only a rookie in Octave :) | | By the way, I got another question to you. Sometimes in the sprintf command | I may use a reference to a function. Everything works fine, but if a put a | keyboard command in the function (to start debug mode) my Octave crashes : | it doesn't let me use the command line and after ctrl+C it crashes . Have | you got any idea? If you think you've found a bug in Octave, please submit a complete bug report to the bug tracker here:   https://savannah.gnu.org/bugs/?func=additem&group=octaveWhen reporting a bug, you need to provide everything necessary for someone else to reproduce the problem.  Please read http://www.octave.org/bugs.html for some tips about how to make a useful bug report. jwe _______________________________________________ Help-octave mailing list Help-octave@octave.org https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Creating "sequential variable names"

 Administrator On 19-Oct-2010, AlbFrigerio wrote: | Thanks a lot jwe, it looks very simple, as all the right things should be. I | implemented it and everything works without using eval or something like | that. | | > I think it is even clearer if you write | > | >   for i = 1:10 | >     AG(i).P.n = 1; | >     AG(i).M.err = 0; | >   endfor | > | > to create a struct array. I should also mention that if you have a large number of elements in your struct array, the above will be somewhat slow because AG will be resized each time through the loop.  It will be faster to write something like   for i = 10:-1:1     ..   end so that resizing happens only once. jwe _______________________________________________ Help-octave mailing list [hidden email] https://www-old.cae.wisc.edu/mailman/listinfo/help-octave