Importing and exporting variables
Engee supports writing and unloading variables in .mat and .jld2 formats:
-
.mat is the format used in MATLAB software.
-
.jld2 is the format used in the Julia programming language.
To write variables to .mat or .jld format, select them in the variables window and right-click to call the context menu. In the menu, click Save as…:
This will open the variable export window:
-
Specify the name of the file that will contain the variables.
-
Select the format of the exported file - MAT or JLD2.
-
Trace the directory where the file will be saved.
-
Set the path where the file will be saved. All folders of the Engee file browser will be displayed in this area.
-
Go back from the current directory to levels above, up to
/user
(the beginning of the Engee file browser path). -
Create a new folder in the directory of step 3.
The saved file in MAT or JLD2 format will appear in the file window . Double left-clicking on the saved file will import the variables into the Engee workspace. The imported variables will be automatically added to the variables window
.
MAT
Use the MAT.jl package to work with .mat data in Julia (see MAT for details). |
You can work with MAT format programmatically, via script editor or command line. For example, consider the following code for reading one variable from MAT:
using MAT # подключаем библиотеку MAT для работы с файлами .mat
a = 1 # задаем переменную а со значением 1
file = matopen("/user/mat_file.mat") # открываем файл mat_file.mat для чтения с помощью функции matopen и создаем объект file
variable_mat = read(file, "a") # считывает переменную "a" из файла типа MAT и присваивает ее значение переменной variable_mat
close(file) # закрывает файл
To write a variable to a MAT file, we use the code:
variable_1 = 1 # зададим переменную variable_1 и присвоим ей значение 1
file = matopen("/user/new_mat_file.mat", "w") # открывает файл new_mat_file.mat в режиме записи ("w")
write(file, "variable_mat", variable_1) # записывает значение переменной variable_1 в переменную variable_mat в файл new_mat_file.mat
close(file) # закрывает файл
With this code we have written a variable to a MAT file. In a similar way, through the matwrite function, we can write a Dict (dictionary) in MAT format, using its keys as variable names:
mat_Dict = Dict(
"variable_1" => 1,
"variable_2" => -2,
"variable_3" => "Hello, world")
matwrite("/user/Dict.mat", mat_Dict)
The code will create a Dict.mat file containing the dictionary with the variable names.
You can use the
|
JLD2
Use the JLD2.jl package to work with .jld2 data in Julia (see JLD2.jl for details ). |
Load the JLD2 module:
import Pkg; #импорт менеджера пакетов
Pkg.add("JLD2") #добавления пакета JLD2
The JLD2.jl package supports function interfaces like jldsave
. For example:
using JLD2 #загрузка модуля
x = 1
y = 2
z = 42
# Простейший случай:
jldsave("example.jld2"; x, y, z)
# это равносильно
jldsave("example.jld2"; x=x, y=y, z=z)
# Новые имена можно присваивать лишь части аргументов
jldsave("example.jld2"; x, a=y, z)
# а чтобы внести полную путаницу, можно сделать так
jldsave("example.jld2"; z=x, x=y, y=z)
If you want to save only one object in a file for later loading, you can use the save_object
and load_object
functions. For example:
save_object(filename, x) #сохраняет объект x в новом JLD2-файле в filename, если файл существует по этому пути, он будет перезаписан.
load_object(filename, x) #загружает объект x