Formulas 9. Data in objects

With square brackets, you can get the values inside some objects.

### Object contents in the calculator

The outcomes of the following examples can be checked with the calculator.

object [objectName\$ or id, rowNumber, columnNumber]
`object ["Matrix hello", 10, 3]`
gives the value in the cell at the third column of the 10th row of the Matrix called hello.
`object [5, 10, 3]`
gives the value in the cell at the third column of the 10th row of the Matrix whose unique ID is 5 (i.e. that is labelled with the number 5 in the list of objects).
`object ["Sound hello", 0, 10000]`
gives the value (in Pa) of the 10000th sample of the Sound hello, averaged over the channels.
`object [23, 1, 10000]`
gives the value (in Pa) of the 10000th sample of the left channel of the Sound with ID 23.
`object [23, 2, 10000]`
gives the value (in Pa) of the 10000th sample of the right channel of the Sound with ID 23.
`object ["TableOfReal tokens", 5, 12]`
gives the value in the cell at the fifth row of the 12th column of the TableOfReal called tokens.
`object ["TableOfReal tokens", 5, "F1"]`
gives the value in the cell at the fifth row of the column labelled F1 of the TableOfReal tokens.
`object ["TableOfReal tokens", "\ct", "F1"]`
gives the value in the cell at the row labelled \ct of column F1 of the TableOfReal tokens.
`object ["Table listeners", 3, "m3ae"]`
gives the numeric value in the cell at the third row of column m3ae of the Table listeners.
`object ["Table listeners", 3, 12]`
gives the numeric value in the cell at the third row of the 12th column of the Table listeners.
`object\$ ["Table results", 3, "response"]`
gives the string value in the cell at the third row of column response of the Table results.
`object\$ ["Table results", 3, 12]`
gives the string value in the cell at the third row of the 12th column of the Table results.
`object ["PitchTier hello", 8]`
gives the pitch (in Hertz) of the 8th point in the PitchTier hello.

Cells (or samples, or points) outside the objects are considered to contain zeroes.

### Interpolation

The values inside some objects can be interpolated.

`object ("Sound hello", 0.7, 0)`
gives the value (in Pa) at a time of 0.7 seconds in the Sound hello, by linear interpolation between the two samples that are nearest to 0.7 seconds. The channels are averaged.
`object ("Sound hello", 0.7, 1)`
gives the interpolated value (in Pa) at a time of 0.7 seconds in the left channel of the Sound hello.
`object ("Sound hello", 0.7, 2)`
gives the interpolated value (in Pa) at a time of 0.7 seconds in the right channel of the Sound hello.
`object ("Spectrogram hallo", 0.7, 2500)`
gives the value at a time of 0.7 seconds and at a frequency of 2500 Hz in the Spectrogram hallo, by linear interpolation between the four samples that are nearest to that point.
`object ("PitchTier hullo", 0.7)`
gives the pitch (in Hertz) at a time of 0.7 seconds in the PitchTier hullo.

In the interpolation, times outside the time domain of the objects are considered to contain zeroes (this does not apply to PitchTiers and the like, which undergo constant extrapolation).

### Object contents in a modification formula

Suppose you want to do the difficult way of reversing the contents of a Sound called hello (the easy way is to choose Reverse from the Modify menu). You select this sound, then choose Copy... to duplicate it to a new Sound, which you name hello_reverse. You select this new Sound and choose Formula... from the Modify menu. The formula will be

``` object ["Sound hello", row, ncol + 1 - col]```

From this example, you see that the indices between [ ] may be formulas themselves, and that you can use implicit attributes like ncol and position references like col (also row, which here means that the reversal is performed for each channel). An alternative formula is

``` object ("Sound hello", xmax - x, y)```

at least if xmin is zero. The advantage of the second method is that it also works correctly if the two sounds have different sampling frequencies; the disadvantage is that it may do some interpolation between the samples, which deteriorates the sound quality (the use of y here means that the reversal is done for all y values, i.e. all channels).

### Object contents in a script

In scripts, the indices between [ ] and the values between ( ) may be formulas themselves and contain variables. The following script computes the sum of all the cells along the diagonal of a Matrix.

``` matrix = Create simple matrix: 10, 10, "x*y"``` ``` sumDiagonal = 0``` ``` for i to object[matrix].ncol``` ```    sumDiagonal += object [matrix, i, i]``` ``` endfor``` ``` writeInfoLine: "The sum of the cells along the diagonal is ", sumDiagonal, "."```

This example could have been written completely with commands from the dynamic menu:

``` matrix = Create simple matrix: 10, 10, "x*y"``` ``` sumDiagonal = 0``` ``` ncol = Get number of columns``` ``` for i to ncol``` ```    value = Get value in cell: i, i``` ```    sumDiagonal += value``` ``` endfor``` ``` writeInfoLine: "The sum of the cells along the diagonal is ", sumDiagonal, "."```

The first version, which accesses the contents directly, is not only two lines shorter, but also three times faster.