# Dictionary Issue when using MAT.jl

**URL:** <https://discourse.julialang.org/t/dictionary-issue-when-using-mat-jl/82334>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 6, 2022, 6:23pm UTC](https://discourse.julialang.org/t/dictionary-issue-when-using-mat-jl/82334 "2022-06-06T18:23:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aditya\_Rai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aditya_rai/32/35586_2.png) [@Aditya\_Rai](https://discourse.julialang.org/u/Aditya_Rai)\
**Post date:** [June 6, 2022, 6:23pm UTC](https://discourse.julialang.org/t/dictionary-issue-when-using-mat-jl/82334/1 "2022-06-06T18:23:22Z")

</div>

Hi Everyone,

I am new to Julia and I was trying to convert some MATLAB code to Julia. The code I am converting uses .MAT file ( a MATLAB file to store workspace variables). In order to load this .MAT file I am using MAT.jl.

I am able to read the .MAT file using MAT.jl however the key value pair I get after reading the file is not what I expected. I have attached the respective output I get when reading the key values pairs.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/7/071ecd03018886a264225c1e386fc4c39b7ecacc.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/6/36db68c4adee4fe134f71a6b6db149bea2ec52e3.png)

I am unable to access the respective values for the keys I am getting.  
Any help in this matter would be appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [June 6, 2022, 10:00pm UTC](https://discourse.julialang.org/t/dictionary-issue-when-using-mat-jl/82334/2 "2022-06-06T22:00:25Z")

</div>

`values` is probably not what you want. Try just accessing that field of the struct directly with index notation, i.e.

```julia
mat_variables["be_data"]

```

Also, welcome! You may also consider joining the #matlabsconders channel on slack or zulip

---

<div class="post-metadata">

**Author:** ![Adi\_Rai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adi_rai/32/34413_2.png) [@Adi\_Rai](https://discourse.julialang.org/u/Adi_Rai)\
**Post date:** [June 7, 2022, 3:31am UTC](https://discourse.julialang.org/t/dictionary-issue-when-using-mat-jl/82334/3 "2022-06-07T03:31:13Z")

</div>

Hi brenhinkeller,

Thanks for your quick response! I tried to access the dictionary using the square bracket notation, however when I enter the respective key in the square brackets I get this error:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/8/586ef7a555d815c2367a80d353d52b005933fc1e.png)

I have also attached the structure of the dictionary if it helps (sorry if it is difficult to read, the cell output was very large hence I had to zoom out):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/2/02311b8f33485b63ac7a66f30ba7b43af8c8efd2.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/f/bf0245bb1e8b9618205cd8f8557a09232dd93747.png)

Thanks for your help again!

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [June 7, 2022, 3:49am UTC](https://discourse.julialang.org/t/dictionary-issue-when-using-mat-jl/82334/4 "2022-06-07T03:49:28Z")

</div>

Ah, I’ve just noticed you’re using `matopen` (which I actually didn’t know existed). I’ve always used `matread` instead, which is what my advice above would apply to.

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [June 7, 2022, 3:49am UTC](https://discourse.julialang.org/t/dictionary-issue-when-using-mat-jl/82334/5 "2022-06-07T03:49:40Z")

</div>

For reference, the built-in documentation (type `?` at the REPL) is pretty good here:

```julia
help?> matread
search: matread matwrite

  matread(filename) -> Dict

  Return a dictionary of all the variables and values in a Matlab file, opening and
  closing it automatically.

```

vs.

```julia
help?> matopen
search: matopen

  matopen(filename [, mode]; compress = false) -> handle
  matopen(f::Function, filename [, mode]; compress = false) -> f(handle)

  Mode defaults to "r" for read. It can also be "w" for write, or "r+" for read or write
  without creation or truncation.

  Compression on reading is detected/handled automatically; the compress keyword
  argument only affects write operations.

  Use with read, write, close, keys, and haskey.

```

```julia
  read(matfile_handle, varname) -> value

  Read a variable from an opened Matlab file and return its value.

  See matopen and matread.

```

---

<div class="post-metadata">

**Author:** ![Adi\_Rai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adi_rai/32/34413_2.png) [@Adi\_Rai](https://discourse.julialang.org/u/Adi_Rai)\
**Post date:** [June 7, 2022, 3:53am UTC](https://discourse.julialang.org/t/dictionary-issue-when-using-mat-jl/82334/6 "2022-06-07T03:53:05Z")

</div>

It worked when using matread, instead of matopen. I should have gone through the documentation better.  
Thanks a lot for your help, I was stuck on this for way too long!

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [June 7, 2022, 5:52am UTC](https://discourse.julialang.org/t/dictionary-issue-when-using-mat-jl/82334/7 "2022-06-07T05:52:26Z")

</div>

Glad to help!
