Reading and writing data to various file types
The purpose of this demo is to show the basic functions for reading data from files and writing data to them.
The structure of the task and file processing is the same: first we write something to a file, and then we read previously recorded data from it.
To begin with, we will declare a common path for all files with the folder of our project.
Pkg.add(["CSV", "MAT"])
path = "$(@__DIR__)/";
CSV
Let's write and read to a CSV file. To do this, we will need the 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 we will implement writing and reading in a text document. In this case, we won'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 the MAT file. To do this, use the MATLAB and MAT libraries. The first supports MATLAB syntax, and the second 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 consider JLD2. These are files that store variables in Engee as lists.
using FileIO
save(path * "example.jld2", Dict("A" => "test", "B" => 12))
load(path * "example.jld2")
b = load(path * "example.jld2", "B")
Conclusion
In this demo, we examined the possibilities of creating and reading the main file types used in Engee.