Reputation: 659
I would like to import a CSV file (file.csv
) as a matrix in Julia to plot it as a heatmap using GR. My CSV file contains 255 rows and 255 entries on each row. Here are some entires from the CSV file to illustrate the format of the rows:
file.csv
-1.838713563526794E-8;-1.863045549663876E-8;-2.334704481052452E-8 ...
-1.7375447279939282E-8;-1.9194929690414267E-8;-2.0258124812468942E-8; ...
⋮
-1.1706980663321613E-8;-1.6244768693064608E-8;-5.443335580296977E-9; ...
Note: The elipsis (...) are not part of the CSV file, rather they indicate that entires have been omitted.
I have tried importing the file as a matrix using the following line m = CSV.read("./file.csv")
, but this results in a 255 by 1 vector rather than the 255 by 255 matrix. Does anyone know of an effective way to import CSV files as matrices in Julia?
Upvotes: 2
Views: 3740
Reputation: 339
While the readdlm
answer by @user:1269567 is simple and clean, it may struggle with very large text files (probably a bad idea to store large numeric data as text anyway, but that's another story) -- which is how I came across this question. An efficient way using no other packages besides CSV.jl
is the following:
using CSV
m = CSV.read("./file.csv", CSV.Tables.matrix; header=false)
as of CSV v0.10.9
, assuming that there are no headers in the file.
Upvotes: 0
Reputation: 1275
Not sure if there has been a change to CSV.jl
, however, if I do CSV.read("file.csv")
it will error
provide a valid sink argument, like 'using DataFrames; CSV.read(source, DataFrame)'
You can however use the fact that it wants any Tables.jl
compatible type:
using CSV, Tables
M = CSV.read("file.csv", Tables.matrix, header=0)
Upvotes: 6
Reputation: 5583
m = CSV.read("./file.csv")
returns a DataFrame
.
If CSV.jl
reads the file correctly so that all the columns of m
are of type Float64
containing no missing
s, then you can convert m
to a Float64
matrix with Matrix{Float64}(m)
, or obtain the matrix with one line:
m = Matrix{Float64}(CSV.read("./file.csv", header=0, delim=';'))
# or with piping syntax
m = CSV.read("./file.csv", header=0, delim=';') |> Matrix{Float64}
readdlm
, though, should normally be enough and first solution to go for such simple CSV files like yours.
Upvotes: 3
Reputation: 69899
You can use
using DelimitedFiles
m = readdlm("./file.csv", ';', Float64)
(last argument specifying type can be omitted if you want Float64
)
Upvotes: 6