Creating "sequential variable names"

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating "sequential variable names"

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?

Thanks everybody, have a nice day,
     Alberto
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

andy buckle
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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

Søren Hauberg
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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

AlbFrigerio
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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

bpabbott
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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

Søren Hauberg
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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

AlbFrigerio
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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

John W. Eaton
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=octave

When 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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

Sergei Steshenko


--- 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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

John W. Eaton
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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

AlbFrigerio
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=octave

When 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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

John W. Eaton
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
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

AlbFrigerio
Wow, I never thought about this fact ... thank you so much!!!

Just a little consideration : I believe the inversion you suggested is necessary only at the first time I create my struct, because from that moment it has the "right" dimensions, hence if I want to modify something (e.g. AG(i).P.n=10) I can use both the "regular" and the inverse loop and I won't have any time difference, isn't it?

Thanks again,
   Alberto

John W. Eaton wrote
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
Help-octave@octave.org
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Creating "sequential variable names"

Jaroslav Hajek-2
In reply to this post by John W. Eaton
On Tue, Oct 19, 2010 at 9:36 AM, John W. Eaton <[hidden email]> wrote:

> 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.

There is also the stack-array optimization designed to combat similar
problems, e.g.
for i = 1:many
  AG(i).P = something(i);
endfor

should *not* reallocate at each step, but once each 1000 steps or so.
In the original example, however, I believe you kill the optimization
by the nested field reference, because the interpreter first has to
perform a an auto_add subsref AG(i).P to check whether AG(i).P might
be a cs-list reference. And unfortunately AFAIK the stack-array
optimization doesn't work with auto_add subsrefs because they make a
copy prior to resizing. It would be nice to overcome this. Maybe once
I have time for Octave again :)

_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave