Using counters in data output


Counters can be useful for labelling elements of data output such as time bins or number of trials etc.

This page will outline how to use counters in Zanscript and provide an example script by way of demonstration. We now have the option for using variables in script which can be more flexible in their use. However, counters can provide a simple way of coding if you are just looking to do basic counting. 

Top

Commands for using counters in Zanscript 

You can have up to 5 different counters in a script. When counters are set they always start at 0

SET(COUNTER1,COUNTER_ZERO)
SET(COUNTER2,COUNTER_ZERO)
SET(COUNTER3,COUNTER_ZERO)
SET(COUNTER4,COUNTER_ZERO)
SET(COUNTER5,COUNTER_ZERO)

Once the counters have been set. The counter can be incremented by 1 using the line of code:

SET(COUNTER1,COUNTER_INC)

Each time the system reads this line of code in a script it will increment the number of the counter by 1.

The counter value can be written into the data file by adding it to the logfield for example:

LOGCREATE("COUNTER1|COUNTER2|COUNTER3|COUNTER4|COUNTER5")
LOGRUN

Top

Example Script

Below is an example script using two counters in an experiment. The first counter numbers the trials. The second counter numbers the time bins for the data output.

SET(COUNTER1,COUNTER_ZERO)
SET(COUNTER2,COUNTER_ZERO)

DEFINE NUM_TRIAL 3
DEFINE NUM_DATA 10
DEFINE TIME_BIN 1

ACTION MAIN

    LOGCREATE("TEXT:RUNTIME|TEXT:TRIAL_NUM|TEXT:TIME_BIN")
    LOGRUN

    INVOKE(TRIAL, NUM_TRIAL)

COMPLETE 

First counters one and two are set to zero. The action main writes the data headings for the data output and then invokes a secondary action called TRIAL and repeats it 3 times. 

ACTION TRIAL

    SET(COUNTER1,COUNTER_INC)

    INVOKE(DATA, NUM_DATA)

COMPLETE

The action TRIAL begins by incrementing counter one by 1 then invoking the action DATA which is repeated 10 times.

ACTION DATA

    SET(COUNTER2, COUNTER_INC)

    LOGDATA(DATA_SNAPSHOT,"begin") 
    WAIT(TIME_BIN)
    LOGDATA(DATA_SNAPSHOT,"end") 
 
    LOGDATA(DATA_SELECT,"begin") 
    LOGDATA(DATA_DELTA,"end")
 
    LOGCREATE("RUNTIME|COUNTER1|COUNTER2")
    LOGRUN()

COMPLETE

The action DATA starts by incrementing counter two by 1 then starting a snapshot of data, waiting 1 second, ending the snapshot of data and then outputting counters one and two into a data file. 

Counter demo data
Example data file from the counter demo script.
Top

Script download

The below script can be downloaded and run on any Zantiks system to demonstrate how counters can be used in Zanscript. 

Zanscript download: counter_demo.zs