0

I am trying to write some data into columns in IDL.

Let's say I am calculating "k" and "k**2", then I would get:

1 1

2 4

3 9

. .

and so on.

If I write this into a file, it looks like this:

1 1 2 4 3 9 . .

My corresponding code looks like this:

pro programname

openw, 1, "filename"
.
. calculating some values
.

printf, 1, value1, value2, value3
close,1
end

best regards

blurfus
  • 13,485
  • 8
  • 55
  • 61
ollowain86
  • 19
  • 4

2 Answers2

0

You should probably read the IDL documentation on formatted output, to get all of the details.

I don't understand your "value1, value2, value3" in your printf. If I were going to do this, I would have two variables "k" and "k2". Then I would print using either a transpose or a for loop:

IDL> k = [1:10]
IDL> k2 = k^2
IDL> print, transpose([[k], [k2]])
       1       1
       2       4
       3       9
       4      16
       5      25
       6      36
       7      49
       8      64
       9      81
      10     100
IDL> for i=0,n_elements(k)-1 do print, k[i], k2[i]
       1       1
       2       4
       3       9
       4      16
       5      25
       6      36
       7      49
       8      64
       9      81
      10     100

By the way, if you are going to use stack overflow you should start "accepting" the correct answers.

Chris Torrence
  • 452
  • 3
  • 11
0

It sounds like you have two vectors. One with single values and one with square values. Here's what you could do:

pro programname

openw, f, "filename", /GET_LUN
.
. calculating some values for k
.
k2 = k^2 ;<---This is where you square your original array.

;Merge the vectors into a single array. Many ways to do this.
k = strtrim(k,2) ;<----------------------------Convert to string format
k2 = strtrim(k2,2) ;<--------------------------Convert to string format
merge = strjoin(transpose([[k],[k2]]),' ') ;<--Merge the arrays

;Now output your array to file the way you want to
for i=0L, n_elements(k)-1 do printf, f, merge[i]

;Close the file and free the logical file unit
free_lun, f

end

cypher
  • 13
  • 1
  • 3