Engee documentation
Notebook

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.

In [ ]:
Pkg.add(["CSV", "MAT"])
In [ ]:
path = "$(@__DIR__)/";

CSV

Let's write and read to a CSV file. To do this, we will need the CSV and DataFrames libraries.

In [ ]:
using CSV, DataFrames
In [ ]:
df = DataFrame(rand(10, 10), :auto)
Out[0]:

10 rows × 10 columns (omitted printing of 2 columns)

x1x2x3x4x5x6x7x8
Float64Float64Float64Float64Float64Float64Float64Float64
10.7826910.175540.2192250.5832330.4524370.9111760.8670760.00160441
20.6760870.7965170.9525820.03084050.9175370.1503310.3680380.697571
30.2917080.2072260.5040470.6810910.767360.2926210.05860940.958023
40.9884170.8803290.2342810.6340550.01694640.9672130.6428220.438598
50.6109940.6973660.03714450.3108670.6059030.1017130.2976520.965925
60.6494290.989730.2157060.8959410.0651160.2819710.7163310.608109
70.9885820.3742470.8433710.6544760.6523370.06218810.3452840.127029
80.1743920.2106490.3570410.6714680.8427050.6264450.6964140.808247
90.2913430.0353250.01641570.1386840.8504540.671060.9769920.495211
100.6805630.6870060.8826470.5824540.3741070.2745180.123540.983522
In [ ]:
CSV.write(path * "data.csv", df)
Out[0]:
"/user/start/examples/base_simulation/reading_and_writing_files/data.csv"
In [ ]:
Matrix(CSV.read(path * "data.csv", DataFrame))
Out[0]:
10×10 Matrix{Float64}:
 0.782691  0.17554   0.219225   0.583233   …  0.00160441  0.389145   0.112194
 0.676087  0.796517  0.952582   0.0308405     0.697571    0.254551   0.983581
 0.291708  0.207226  0.504047   0.681091      0.958023    0.448821   0.755106
 0.988417  0.880329  0.234281   0.634055      0.438598    0.663057   0.306334
 0.610994  0.697366  0.0371445  0.310867      0.965925    0.0795114  0.112329
 0.649429  0.98973   0.215706   0.895941   …  0.608109    0.645418   0.290919
 0.988582  0.374247  0.843371   0.654476      0.127029    0.83555    0.695415
 0.174392  0.210649  0.357041   0.671468      0.808247    0.968789   0.507406
 0.291343  0.035325  0.0164157  0.138684      0.495211    0.267949   0.522488
 0.680563  0.687006  0.882647   0.582454      0.983522    0.865961   0.312841

TXT

Now we will implement writing and reading in a text document. In this case, we won't need any third-party libraries.

In [ ]:
write(path * "data.txt", "Пример")
Out[0]:
12
In [ ]:
open(io->read(io, String), path * "data.txt")
Out[0]:
"Пример"

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.

In [ ]:
using MATLAB, MAT
In [ ]:
mat"p = genpath($path); addpath(p);"
In [ ]:
a = [1 2 3]
mat"a=$a"
mat"save($path + string('data.mat'),'a')"
In [ ]:
b = matopen(path * "data.mat")
read(b, "a")
Out[0]:
1×3 Matrix{Int64}:
 1  2  3

JLD2

Now consider JLD2. These are files that store variables in Engee as lists.

In [ ]:
using FileIO
In [ ]:
save(path * "example.jld2", Dict("A" => "test", "B" => 12))
In [ ]:
load(path * "example.jld2")
Out[0]:
Dict{String, Any} with 2 entries:
  "B" => 12
  "A" => "test"
In [ ]:
b = load(path * "example.jld2", "B")
Out[0]:
12

Conclusion

In this demo, we examined the possibilities of creating and reading the main file types used in Engee.