# How to read variables of .mat in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-read-variables-of-mat-in-julia/84193>\
**Category:** New to Julia\
**Tags:** question, matlab\
**Created:** [July 13, 2022, 8:18pm UTC](https://discourse.julialang.org/t/how-to-read-variables-of-mat-in-julia/84193 "2022-07-13T20:18:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raamsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raamsa/32/33488_2.png) [@raamsa](https://discourse.julialang.org/u/raamsa)\
**Post date:** [July 13, 2022, 8:18pm UTC](https://discourse.julialang.org/t/how-to-read-variables-of-mat-in-julia/84193/1 "2022-07-13T20:18:25Z")

</div>

I have installed MAT.jl package to read variables from .mat file.

When I use the following syntax. I got the error. How to resolve it?

```julia
using MAT

file = matopen("subject3455session6.mat")

read(file, "varname")
close(file)

```

Info: Precompiling MAT [23992714-dd62-5051-b70f-ba57cb901cac]  
└ @ Base loading.jl:1260  
WARNING: could not import HDF5.exists into MAT  
File “subject3455session6.mat” does not exist and create was not specified

Stacktrace:  
[1] error(::String) at ./error.jl:33  
[2] matopen(::String, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool) at /home/rakesh/.julia/packages/MAT/wBZA8/src/MAT.jl:44  
[3] matopen(::String, ::String; compress::Bool) at /home/rakesh/.julia/packages/MAT/wBZA8/src/MAT.jl:88  
[4] matopen at /home/rakesh/.julia/packages/MAT/wBZA8/src/MAT.jl:88 [inlined]  
[5] #matopen#2 at /home/rakesh/.julia/packages/MAT/wBZA8/src/MAT.jl:97 [inlined]  
[6] matopen(::String) at /home/rakesh/.julia/packages/MAT/wBZA8/src/MAT.jl:97  
[7] top-level scope at In[1]🥈

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [July 13, 2022, 8:35pm UTC](https://discourse.julialang.org/t/how-to-read-variables-of-mat-in-julia/84193/2 "2022-07-13T20:35:19Z")

</div>

I think Julia’s current directory might be in the wrong location? Try running `pwd()` to see if that file path contains “subject3455session6.mat”.

---

<div class="post-metadata">

**Author:** ![raamsa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raamsa/32/33488_2.png) [@raamsa](https://discourse.julialang.org/u/raamsa)\
**Post date:** [July 14, 2022, 7:37pm UTC](https://discourse.julialang.org/t/how-to-read-variables-of-mat-in-julia/84193/3 "2022-07-14T19:37:34Z")

</div>

Thanks for your response!

Sorry for the late reply. Your suggestion works but I am stuck in ploting the variables in .mat files. There are two different types of variables in the file , struct and cell arrays.

Could you help to know the syntax to plot these variables?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [July 15, 2022, 7:22am UTC](https://discourse.julialang.org/t/how-to-read-variables-of-mat-in-julia/84193/4 "2022-07-15T07:22:54Z")

</div>

It’s very difficult to give a “one size fits all” answer, because structs and cell arrays are very general data types. If, for instance, each cell in the cell array contains a number, it would be appropriate to import it as or convert it to an `Array{Float64}`.
