Department of Physics and Astronomy
Bucknell University

IDL Tutorial: Variables

The whole point of IDL is to store data in array variables and easily process them. There are three kinds of variables:

  1. Scalar variables: store a single number or character string.

  2. Array variables, store a vector or array of numbers or strings

  3. Structured variables: mixed arrays that are accessed by tag names. We won't be using these very much, except when called for by some IDL routine.

Scalar Variables

To set and view a scalar variable, try the following examples:

x = 3.14

print, x

x = !pi

print, x

x = round(!pi)

print, x

x = sin(!pi / 2)

print, x

x = 'Hello, World'

print, x

Some things to notice:

  • When you run a command, or procedure, you separate the command and any arguments by commas.

  • System variables (i.e., built-in variables) are accessed by a preceding exclamation point.

  • Like in other programming languages, there are built-in functions like sin, exp, tan, etc. They return values, their arguments must be in parentheses, and multiple arguments are separated by commas.

Array Variables

Arrays (lists of numbers) are defined and accessed by using square brackets. Try some of the following examples, and comment on the behavior.

x = [0., 1.0, 2.0, !pi]

print, x

print, sin(x)

print, x^3

x = [[0.,1.0], [2.0, !pi]]

print, x, sin(x)

You should have noticed that, by default, functions operate on every number in an array. This is one of the primary advantages of IDL: you don't have to write DO loops to perform simple operations on data. There are however FOR loops available for more sophisticated operations.

Now try these string arrays:

sourcelist = ['NGC1068', 'NGC3079', 'NGC4151']

ra = [2.0, 2.5, 4.5]

dec = [0.0, 55., 30.]

print, sourcelist[1], ' ', ra[1], dec[1]

Try to explain what happened there.

Notice that you access scalar elements of arrays by using square brackets and the index number. Indices start at 0 and run the length of the array. To see how many data are stored in an array, you can use the n_elements or size functions.

A More Interesting Example

Try the following, and use the help function (?) to figure out what is going on.

x = findgen(1000)/999. * 2.0 * !pi

y = 10.0 + 2.0 * sin(x)

print, x[0], y[0]

print, x[n_elements(x)-1], y[n_elements(x)-1]

plot, x, y, xtitle='X', ytitle='Y'

Concatenating Arrays

Sometimes you have an array that you want to add new values to - you want to "grow" the array. Let's take first the simple case of adding a single (scalar) value to an array.

y = [1.3, 2.7, 9.5]

Suppose y contains a list of signal measurements that needs to be updated as new measurements come in. To add a new value to the list, say, 10.7, you concatenate this value to the array:

y = [y, 10.7]

By putting an array into a new array, you are explicitly adding new elements to it. Try this example, followed by print, y.

Here's another neat trick. Suppose we start with,

y = [1.3, 2.7, 9.5]

but now, instead of adding to the end of this array, we want to add another row of data to the array. Here's an example of how to do it.

x = [10.7, 12.2, 15.4]

y = [[y], [x]]

print, y

Try it. By adding extra square brackets, you change the dimension of the array that you are concatenating.

Getting Information about Variables

After a while of working away in IDL, it's easy to lose track of what variables are defined. If you're ever curious about them, try entering the simple command:

help

This command will provide an exhaustive list of defined variables and compiled procedures. If you want information about a specific variable,

help, y

In my case, this command produced the following output:

Y FLOAT = Array[3, 2]

indicating that y is a floating point array with dimensions of 3 columns by 2 rows.

Further Reading


Back to Index

Next Tutorial


Last modified by Jack Gallimore.