# Write the output from ODE solve to a file

**URL:** https://discourse.julialang.org/t/write-the-output-from-ode-solve-to-a-file/62390
**Category:** New to Julia
**Tags:** question, diffeq
**Created:** [June 4, 2021, 1:17pm UTC](https://discourse.julialang.org/t/write-the-output-from-ode-solve-to-a-file/62390 "2021-06-04T13:17:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Deepa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deepa/32/23385_2.png) [@Deepa](https://discourse.julialang.org/u/Deepa)
#### Post date: [June 4, 2021, 1:17pm UTC](https://discourse.julialang.org/t/write-the-output-from-ode-solve-to-a-file/62390/1 "2021-06-04T13:17:29Z")

</div>

Hi All,

I’d like to know how to write the output from `solve` to a file. I’d prefer to write the solution `sol` to a  
text or .mat file.

```julia
prob= ODEProblem(ODEFunction(fun, sparsity=sparsity,x0,(0.0,5.0))
@btime sol = solve($prob,QNDF())

```

Suggestions will be really appreciated.

---

<div class="post-metadata">

### Author: ![frankschae](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankschae/32/8382_2.png) [@frankschae](https://discourse.julialang.org/u/frankschae)
#### Post date: [June 4, 2021, 2:45pm UTC](https://discourse.julialang.org/t/write-the-output-from-ode-solve-to-a-file/62390/2 "2021-06-04T14:45:17Z")

</div>

Saving the solution as a BSON or CSV is probably the most convenient:  
[https://diffeq.sciml.ai/latest/features/io/#io](https://diffeq.sciml.ai/latest/features/io/#io)  
if you really want a plain txt file, you could do so with  
[I/O and Network · The Julia Language](https://docs.julialang.org/en/v1/base/io-network/#Base.open) and a loop over `sol`.

```julia

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [June 4, 2021, 2:59pm UTC](https://discourse.julialang.org/t/write-the-output-from-ode-solve-to-a-file/62390/3 "2021-06-04T14:59:09Z")

</div>

> [@frankschae](#):
>
> if you really want a plain txt file, you could do so with  
> [I/O and Network · The Julia Language](https://docs.julialang.org/en/v1/base/io-network/#Base.open) and a loop over `sol` .

CSV is plain text. You can use `dlmwrite` from the `DelimitedFiles` standard library or, for greater performance, [CSV.jl](https://github.com/JuliaData/CSV.jl).

The [MAT.jl](https://github.com/JuliaIO/MAT.jl) package can write Matlab files, if you need that, but I wouldn’t recommend using that format except for interchange with Matlab. More generally, you can use [HDF5.jl](https://github.com/JuliaIO/HDF5.jl) as a binary interchange format.

---

<div class="post-metadata">

### Author: ![Deepa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deepa/32/23385_2.png) [@Deepa](https://discourse.julialang.org/u/Deepa)
#### Post date: [June 4, 2021, 5:07pm UTC](https://discourse.julialang.org/t/write-the-output-from-ode-solve-to-a-file/62390/4 "2021-06-04T17:07:24Z")

</div>

@frankschae @stevengj

```julia
prob= ODEProblem(ODEFunction(fun, sparsity=sparsity,x0,(0.0,5.0))
@btime sol = solve($prob,QNDF())
df = DataFrame(sol)
println(df)
CSV.write("out.csv",df)

```

I tried the above. I’m not sure what’s the right way to access `sol` in the output `@btime sol = solve($prob,QNDF()) `.  
I get the following error in the line `df = DataFrame(sol) `

```julia
ERROR: LoadError: UndefVarError: sol not defined
Stacktrace:

```

Suggestions on the right way to access the data present in the variable `sol` will be really helpful.

---

<div class="post-metadata">

### Author: ![frankschae](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankschae/32/8382_2.png) [@frankschae](https://discourse.julialang.org/u/frankschae)
#### Post date: [June 4, 2021, 9:10pm UTC](https://discourse.julialang.org/t/write-the-output-from-ode-solve-to-a-file/62390/5 "2021-06-04T21:10:31Z")

</div>

I think your code would work by putting the `@btime` macro to the rhs

```julia
sol = @btime solve($prob,QNDF())

```

However, if you have benchmarked the solvers already and if you only want to store the solution of the ODE solve and output a rough timing at the same time, then you could also use `@time` :

```julia
sol = @time solve(prob,QNDF())

```

which won’t run the `solve` call several times.
