# Why are MAT files created by Julia significantly larger than those created by Matlab?

**URL:** <https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 15, 2021, 1:14am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793 "2021-10-15T01:14:20Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 15, 2021, 1:14am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/1 "2021-10-15T01:14:21Z")

</div>

This is how I created my MAT file

```julia
using MAT
F1 = matopen("test.mat", "w");
write(F1, "x", B[:,1]);
write(F1, "y", B[:,2]);
write(F1, "f", B[:,3]);
close(F1)

```

It is 898MB. If I open it within Matlab and then  
`save('test2.mat', 'x', 'y', 'f')`  
My new file storing the same variables is only 127MB.

I tried to regenerate the file from scratch inside Matlab, and again the file size is only 127MB.

Why?

What did I do wrong?

Thanks!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 15, 2021, 1:17am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/2 "2021-10-15T01:17:17Z")

</div>

MATLAB has some kind of default compression?

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [October 15, 2021, 1:45am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/3 "2021-10-15T01:45:42Z")

</div>

The MAT.jl repo is where this question is best asked, but I think they now use some form of hdf5 so compression is likely the reason.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 15, 2021, 2:30am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/4 "2021-10-15T02:30:11Z")

</div>

> [@jling](#):
>
> MATLAB has some kind of default compression?

Yep: [MAT-File Versions - MATLAB & Simulink](https://www.mathworks.com/help/matlab/import_export/mat-file-versions.html)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [October 15, 2021, 2:53am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/5 "2021-10-15T02:53:57Z")

</div>

There are some details about the compression used here:

> **[Improving save performance - Undocumented Matlab](https://undocumentedmatlab.com/articles/improving-save-performance)**
>
> There are many different ways of improving Matlab's standard save function performance.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [October 15, 2021, 6:44am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/6 "2021-10-15T06:44:52Z")

</div>

I think MAT.jl doesn’t use compression by default. There’s a kwarg you need to add: `compress = true`  
I looked into the test folder: [https://github.com/JuliaIO/MAT.jl/blob/3ed629c05f7261e86c0dde0869d265e99a265efb/test/write.jl#L22](https://github.com/JuliaIO/MAT.jl/blob/3ed629c05f7261e86c0dde0869d265e99a265efb/test/write.jl#L22)  
Example: `matwrite(joinpath(new_dir, fname), data; compress=true)`

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 15, 2021, 7:44am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/7 "2021-10-15T07:44:11Z")

</div>

Amusing point: the `savefast` code the author links is mine back from my pre-Julia days. It now feels slightly weird seeing code from those days.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 15, 2021, 9:08am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/8 "2021-10-15T09:08:07Z")

</div>

Thank you all for the very helpful discussion!

Tim,  
Interesting to hear about that.

Concerning the best way to write the code, I assume you meant the below?  
matwrite(F1, Dict(  
“x” =\> B[:,1],  
“y” =\> B[:,2],  
“f” =\> B[:,3]  
); compress = true);

My question is why they use a semicolon, instead of a comma after the dictionary variable? Both seem to work though.

---

<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:** [October 15, 2021, 9:19am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/9 "2021-10-15T09:19:01Z")

</div>

Semicolon can be used to delimit positional arguments from keyword ditto. It’s not necessary here, but both bluestyle and yas recommend it. And there is this feature, where it does matter:

```julia
a = true
f(x; a) == f(x, a=true) != f(x, a)

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 15, 2021, 11:25am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/10 "2021-10-15T11:25:09Z")

</div>

> [@tim.holy](#):
>
> Amusing point: the `savefast` code the author links is mine back from my pre-Julia days.

You made MATLAB fast in your pre-Julia days, then you made the same in Julia, just slower… 😵 That’s unusual for you.

[I’m really not complaining. your MAT.jl really helped me, port MATLAB code to Julia at my job. It was my first assignment, and I had used MATLAB (or Octave actually) a bit before but never professionally nor Julia.]

It’s probably easy to enable compression for MAT.jl? [Not pushing for it, I don’t need .mat files, they just helped be temporarily while I needed to run both Julia and MATLAB while porting, until I completely got rid of all MATLAB code.]

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 15, 2021, 11:56am UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/11 "2021-10-15T11:56:28Z")

</div>

> [@Palli](#):
>
> It’s probably easy to enable compression for MAT.jl?

> <https://github.com/JuliaIO/MAT.jl/blob/3ed629c05f7261e86c0dde0869d265e99a265efb/src/MAT.jl#L142>

it’s already there

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 15, 2021, 12:10pm UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/12 "2021-10-15T12:10:47Z")

</div>

IMO, `compress=true` should be the default. I really do not see the benefit of an uncompressed MAT file. It just takes so much of my disk space.

Kudos to Tim for creating the MAT.jl package! Many thanks 👍

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 15, 2021, 1:09pm UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/13 "2021-10-15T13:09:42Z")

</div>

I didn’t really write MAT.jl, because by the time I helped get HDF5 and JLD off the ground I was pretty solidly in the Julia camp. So why would I need to save `*.mat` files anymore? Making MAT better is now up to people who need it.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 12, 2021, 2:31pm UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/14 "2021-12-12T14:31:56Z")

</div>

By now, I’m pretty sure the reason MAT files created by Julia are so large is because MAT.jl stores info in the format of a Dictionary, which takes up an order of magnitude more disk space than Arrays or Tuples.

I wonder if they could upgrade the package so that they will use named tuples to store the variables instead. Named tuples takes up as little space as a regular tuple, and is the most efficient of storing variables.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 12, 2021, 4:28pm UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/15 "2021-12-12T16:28:14Z")

</div>

I think HDF5 files (which is what .mat file really is) are compressed by default in Matlab, but not in Julia. Enable compression when writing the file.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 13, 2021, 3:03pm UTC](https://discourse.julialang.org/t/why-are-mat-files-created-by-julia-significantly-larger-than-those-created-by-matlab/69793/16 "2021-12-13T15:03:33Z")

</div>

Thanks! I’ve been compressing them, but it is still slightly larger than files created by Matlab.
