1

I know this is a beginnerquestion but i'm having problems in saving my data into a CSV file for example like in this code:

function mar(x,y)
for i in 1:10
s=x*i+y
end
end

how to save sand xand y i just would like to know the procedure that's all.

1 Answer 1

3

You can use the CSV.jl library, which includes the CSV.write function. Example:

julia> using CSV                                       

julia> x = rand(10); y = rand(10); s = (1:10).*x .+ y;

julia> CSV.write("out.csv", (s = s, x = x, y = y)) 

Here I'm constructing a NamedTuple from s, x, and y, which satisfies the Tables.jl interface and therefore can be written out to file by CSV.jl like any other table.

Sign up to request clarification or add additional context in comments.

4 Comments

what if i want to do it inside a function in a more simple way
Not sure what you mean? You can of course call CSV.write in a function in the same way? CSV is a tabular data format, so two things you likely won't get around are (1) creating some 2-dimensional structure to hold the data you want to write and (2) actually calling some sort of write function. That's all that's happening in my example above, so not sure how to make it "more simple"?
i mean whithout changing the example of the code that i put in the question because actually the real code is quite big and i can't change its shape
I constructed s, x and y slightly differently from your code to make it a working example, but you can just stick CSV.write("out.csv", (s = s, x = x, y = y)) after your for loop and it should work, no?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.