Reading and writing data to different types of files¶
The purpose of this demonstration is to show the basic functions for reading data from and writing data to files.
The structure of file assignment and processing is the same: first we write something to a file, and then we read previously written data from it.
To begin with, let's declare a common path for all files with the folder of our project.
Pkg.add(["CSV", "MAT"])
path = "$(@__DIR__)/";
CSV¶
Let's perform writes and reads to a CSV file. For this purpose we need CSV and DataFrames libraries.
using CSV, DataFrames
df = DataFrame(rand(10, 10), :auto)
CSV.write(path * "data.csv", df)
Matrix(CSV.read(path * "data.csv", DataFrame))
TXT¶
Now let's implement writing and reading to a text document. In this case we don't need any third-party libraries.
write(path * "data.txt", "Пример")
open(io->read(io, String), path * "data.txt")
MAT¶
Now let's read and write to MAT-file. For this purpose we will use MATLAB and MAT libraries. The first one supports MATLAB syntax, and the second one simplifies interaction with MATLAB files.
using MATLAB, MAT
mat"p = genpath($path); addpath(p);"
a = [1 2 3]
mat"a=$a"
mat"save($path + string('data.mat'),'a')"
b = matopen(path * "data.mat")
read(b, "a")
JLD2¶
Now let's look at JLD2. These are files that store variables as lists in Engee.
using FileIO
save(path * "example.jld2", Dict("A" => "test", "B" => 12))
load(path * "example.jld2")
b = load(path * "example.jld2", "B")
Conclusion¶
In this demonstration we have looked at the ability to create and read the main file types used in Engee.