Search code examples
arrayslistelementadditionidl-programming-language

How to add elements to an array in IDL that I can use to plot?


I am writing this in IDL.

I want to pass a list of numbers in array x that goes through function y, where then all y values go into an array z, that will be plotted x vs. z.

I have tried to solve this numerous ways. Initially, I was using lists, where it is easy to use the 'list.add' function to add new elements to an array. However, I can only plot arrays in IDL, and not list (thus getting an error). I have been messing around with the code for quite some time, so hopefully I can get some pointers in the right direction

z = []        ; array to hold y data

x = [1,2,3,4] ; x data

FOREACH element, x DO BEGIN

  y = x+3
  z.add,y

ENDOFREACH

; the plot

p = SCATTERPLOT(x,z)

I am hoping to get a simple scatter plot from this, but I do not know how to change my arrays.


Solution

  • There are two main ways to do this:

    1. Concatenate arrays. For example, y = [y, 3] tacks on a 3 at the end of the y array. This is slow if you intend to this "a lot".

    2. Use a list for building the list of values, then use the toArray() method at the end to get and array which can be plotted.