1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# User input
s = readline()
x = parse(Float64, readline())
n = parse(Int, readline())
# DelimitedFiles (lightweight, no DataFrame needed)
using DelimitedFiles
# Example: solver returns two real numbers per x
solver(x) = (sin(x), cos(x))
x_arr = range(0, 1; length=10)
# Rows = samples; columns = x plus two solver outputs
# good practice to know the dimensions of data
data = zeros(length(x_arr), 3)
for (i, x_now) in enumerate(x_arr)
data[i, :] = [x_now, solver(x_now)...]
end
# "w" overwrites an existing file; do-block closes it automatically
open("result.dat", "w") do io
println(io, "# x\ty\tz")
writedlm(io, data, '\t')
end
data2 = readdlm("result.dat"; comments=true)
xvec = data2[:, 1]
yvec = data2[:, 2]
zvec = data2[:, 3]
# alternative method for datafiles
using CSV, DataFrames
# Named columns in an ordinary CSV
df = DataFrame(x=xvec, y=yvec, z=zvec)
CSV.write("result.csv", df)
df2 = CSV.read("result.csv", DataFrame)
# Read the tab-delimited .dat file above
df3 = CSV.read("result.dat", DataFrame;
delim='\t', comment="#", header=["x", "y", "z"])
# Load a Julia file
include("./myfile.jl")
|