# Converting MATLAB ".mat" matrices to julia matrices

**URL:** <https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295>\
**Category:** New to Julia\
**Created:** [September 14, 2022, 11:19pm UTC](https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295 "2022-09-14T23:19:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [September 14, 2022, 11:19pm UTC](https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295/1 "2022-09-14T23:19:26Z")

</div>

Hello everyone,

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 appriciated. Thanks

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 14, 2022, 11:44pm UTC](https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295/2 "2022-09-14T23:44:56Z")

</div>

Have you tried `matread`? I.e.,

```julia
vars = matread("A.mat")

```

---

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [September 14, 2022, 11:50pm UTC](https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295/3 "2022-09-14T23:50:43Z")

</div>

> [@briochemc](#):
>
> `vars = matread("A.mat")`

Thanks. Just tried it. vars becomes a dictionary, not a matrix. I wanted A.mat to be a matrix also in Julia

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [September 14, 2022, 11:53pm UTC](https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295/4 "2022-09-14T23:53:42Z")

</div>

Hi Muhammad, welcome to Julia community. You write:

> [@Muhammad\_Nadeem](#):
>
> when I use use it as: A = matopen(“A.mat”) in julia then

But note that such usage does not follow the instructions given on the [MAT.jl](https://github.com/JuliaIO/MAT.jl) page:

> To read a single variable from a MAT file (compressed files are detected and handled automatically):

```julia
file = matopen("matfile.mat")
read(file, "varname") # note that this does NOT introduce a variable ``varname`` into scope
close(file)

```

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [September 15, 2022, 12:00am UTC](https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295/5 "2022-09-15T00:00:30Z")

</div>

In Matlab:

```matlab
>> A = [1 2; 3 4]

A =

     1 2
     3 4

>> save file_with_A

```

In Julia:

```julia
julia> using MAT

julia> file = matopen("file_with_A.mat")
MAT.MAT_v5.Matlabv5File(IOStream(<file file_with_A.mat>), false, #undef)

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

julia> close(file)

```

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 15, 2022, 12:06am UTC](https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295/6 "2022-09-15T00:06:06Z")

</div>

Note that `A.mat` is a file in “MAT” format, and _ **not** _ a matrix. In MATLAB, if you do

```matlab
>> vars = load("A.mat")

```

you get a MATLAB `struct` containing the variables you saved. If the `A.mat` file contains a matrix `A`, then you can access it in MATLAB with

```matlab
>> A = vars.A % or directly with A = load("A.mat").A

```

In Julia, you would do

```julia
julia> A = vars["A"] # or directly A = matread("A.mat")["A"]

```

That’s because MATLAB’s `struct`s are essentially the same as Julia dictionaries, albeit with the syntactic difference to grab objects inside (i.e., `vars.A` in MATLAB vs `vars["A"]` in Julia).

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 15, 2022, 3:35am UTC](https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295/7 "2022-09-15T03:35:51Z")

</div>

Linking to [Converting MATLAB ".mat" matrices to Julia Matrices - Stack Overflow](https://stackoverflow.com/questions/73724123/converting-matlab-mat-matrices-to-julia-matrices)

---

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [September 15, 2022, 4:35pm UTC](https://discourse.julialang.org/t/converting-matlab-mat-matrices-to-julia-matrices/87295/8 "2022-09-15T16:35:42Z")

</div>

Thank you so much
