Scripting 5.1. Variables

A variable is a location in your computer’s memory that has a name and where you can store something, as explained in §3.2 and §3.4. In a Praat script, you can store numbers and texts, i.e. you can use numeric variables and string variables.

Numeric variables

Numeric variables can hold integer numbers between -1,000,000,000,000,000 and +1,000,000,000,000,000 or real numbers between -10308 and +10308. The smallest numbers lie near -10-308 and +10-308.

You use numeric variables in your script like this:

    length = 10
    Draw line: 0, length, 1, 1

This draws a line in the Picture window from position (0, 10) to position (1, 1). In the first line, you assign the value 10 to the variable called length, and in the second line you use the value of length as the second argument to the command Draw line....

Names of numeric variables must start with a lower-case letter, optionally followed by a sequence of letters, digits, and underscores.

String variables

You use string variables, which contain text, as follows:

    title$ = “Dutch nasal place assimilation”
    Text top: “yes”, title$

This writes the text "Dutch nasal place assimilation" above your drawing.

As in the programming language Basic, the names of string variables end in a dollar sign.

Making numeric variables visible

You can write the content of numeric variables directly to the info window:

    x = 2.0
    root = sqrt (x)
    writeInfoLine: "The square root of ", x, " is ", root, "."

This will write the following text to the Info window:

    The square root of 2 is 1.4142135623730951.

You can fix the number of digits after the decimal point by use of the fixed$ function:

    x = 2.0
    root = sqrt (x)
    writeInfoLine: "The square root of ", fixed$ (x, 3), " is approximately ", fixed$ (root, 3), "."

This will write the following text to the Info window:

    The square root of 2.000 is approximately 1.414.

By using 0 decimal digits, you round to whole values:

    root = sqrt (2)
    writeInfoLine: "The square root of 2 is very approximately ", fixed$ (root, 0), "."

This will write the following text to the Info window:

    The square root of 2 is very approximately 1.

By using the percent$ function, you give the result in a percent format:

    jitter = 0.0156789
    writeInfoLine: "The jitter is ", percent$ (jitter, 3), "."

This will write the following text to the Info window:

    The jitter is 1.568%.

The number 0, however, will always be written as 0, and for small numbers the number of significant digits will never be less than 1:

    jitter = 0.000000156789
    writeInfoLine: "The jitter is ", percent$ (jitter, 3), "."

This will write the following text to the Info window:

    The jitter is 0.00002%.

Predefined variables

All of the variables you saw earlier in this tutorial were defined at the first moment a value was assigned to them. Some variables, however, are already defined implicitly at the start of your script.

Some predefined numeric variables are macintosh, windows, and unix, which are 1 if your edition of Praat was built for the Macintosh, Windows, or Unix platform (respectively), and which are otherwise zero. Likewise, we have praat_32bit and praat_64bit, of which one is 1 and the other 0, depending on whether your edition of Praat was built for 32-bit or 64-bit computers. More precisely, we have praat_intel32, praat_intel64 and praat_arm64, one of which is 1 (and the others 0) depending on whether the type of processor chip that your edition of Praat was made for is 32-bit Intel (= x86, i386, i686), or 64-bit Intel (= AMD64), or ARM64 (= Aarch64). Another predefined numeric variable is praatVersion, which is e.g. 6407 for the current version of Praat.

Some predefined string variables are newline$, tab$, and shellDirectory$. The last one specifies the folder that was the default folder when Praat started up; you can use it in scripts that run from the Unix or Windows command line. Likewise, there exist the predefined string variables homeDirectory$, preferencesDirectory$, and temporaryDirectory$. These three refer to your home folder (which is where you log in), the Praat preferences folder, and a folder for saving temporary files; if you want to know what they are on your computer, try to write them into a script window. The variable defaultDirectory$ is available for formulas in scripts; it is the folder that contains the script file. Finally, we have praatVersion$, which is “6.4.07” for the current version of Praat.

Functions that handle variables

To check whether a variable exists, you can use the function

    variableExists (variableName$)

Links to this page


© ppgb 20201229;20230416;20240103