Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting MATLAB ".mat" matrices to Julia Matrices

Tags:

julia

I am new to Julia. I have been using MATLAB for quite a while. I have few matlab matices namely, A.mat, B.mat, C.mat and so on. I have to read these matrices into Julia and then do some optimization using them. I dont know how to convert these matlab matrices to Julia matrices. I have used the pkg MAT.jl. when I use use it as: A = matopen("A.mat") in julia then "A" is not a matrix in julia. How do I read convert these matrices to julia matrices. Any help will be appreciated. Thanks

like image 253
Muhammad Nadeem Avatar asked Oct 15 '25 12:10

Muhammad Nadeem


1 Answers

You need to use the read function

For this examples I will use the array.mat that comes in MAT.jl test suite.

julia> using MAT

julia> ff = matopen(joinpath(pathof(MAT),"../..", "test/v7/array.mat"));

julia> read(ff,"a2x2")
2×2 Matrix{Float64}:
 1.0  3.0
 4.0  2.0

If you want to define it as a variable in your namespace you can used assignment or the @read macro:

julia> @read ff a2x2;

julia> a2x2
2×2 Matrix{Float64}:
 1.0  3.0
 4.0  2.0

Last but not least you might want to see the list of variables in your mat file:

julia> keys(read(ff))
KeySet for a Dict{String, Any} with 6 entries. Keys:
  "empty"
  "string"
  "a2x1"
  "a2x2x2"
  "a2x2"
  "a1x2"
like image 200
Przemyslaw Szufel Avatar answered Oct 17 '25 09:10

Przemyslaw Szufel