Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

logging variables in a while-loop

edited March 2017 in OpenSesame

Hi guys,
I need help again :blush:
In a while loop, I use a variable which gets overwritten after every increment of the loop.
The variable needs to be logged in the logfile in every loop. For example:

    i=0
    while i<3:
            var.response_time= i
            i=i+1

In this example the var.response_time has three diefferent phases (0,1,2). I want to log all these phases.
The problem is:
With log.write_vars() a lot off unnecessary things get written in the logfile.

    i=0
    while i<3:
            var.response_time= i
            log.write_vars()
            i=i+1

With log.write(var.response_time) the entrys of every loop is under the other. For this example, this isn't a problem, but in my experiment with parallel loops it is not good when every entry of the diefferent variables in the loop comes after the other.

What I want:
var1 var2 var3
x1 y1 z1
x2 y2 z2
. . .
. . .

What I get with log.write()
x1
y1
x2
z1
y2
x3
.
.
.
I hope you guys can help me :)

Comments

  • Hi Flippy,

    In your case, you want to change the name of the variable on every iteration of the loop. In other words, the name of the variable is itself variable. You can do this using the var.set() function, which takes the name of the variable as the first argument, and the value as the second:

    for i in range(3):
        var.set('my_var_%d' % i, i)
    log.write_vars()    
    

    This will result in a column-based log file like:

    my_var_0  my_var_1  my_var_2
    0         1         2
    

    For more information, see:

    Does that answer your question?

    Cheers,
    Sebastiaan

Sign In or Register to comment.