00koeffers
00koeffers

Reputation: 327

Create a table and save it into a csv file in Julia

If a loop creates a lot of output, printing it out in the console gets quite confusing. To solve this problem, I want to save the output in a table and then print it into a csv file.

This is a simplified version of the code that I have tried:

n=2
output = Any[0 for i in 1:(1+n*2), j in 1:3] #table in which output should be saved
output[1,1]="run"
output[1, (1+i):(1+i+1) for i in 1:n] = ["A"i "B"i]

for run in 1:2
    output[1, run+1] = run
    output[2:width(output), run+1] = 1:(width(output)-1)
end

writecsv("C:/Users/user1/Desktop/output.csv",output)

The output should look like:

run___A1____B1____A2___B2

1_____1_____2_____3_____4

2_____1_____2_____3_____4

However, I get an error in the foutht line: syntax: missing separator in array expression. I do understand that it seems to have a problem with my syntax, however as far as I see it, it is correct that way.

Upvotes: 0

Views: 221

Answers (1)

Korsbo
Korsbo

Reputation: 724

When you are creating your output matrix, Julia automatically specialises it to a matrix of Ints. What you can do is to explicitly tell Julia to create a matrix with the element type Any

To do so, replace

output = [0 for i in 1:(1+n*2), j in 1:3] 

with

output = Any[0 for i in 1:(1+n*2), j in 1:3] 

Upvotes: 1

Related Questions