# NetCDF file created in Matlab is read differently by Julia?

**URL:** <https://discourse.julialang.org/t/netcdf-file-created-in-matlab-is-read-differently-by-julia/69162>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 4, 2021, 2:33am UTC](https://discourse.julialang.org/t/netcdf-file-created-in-matlab-is-read-differently-by-julia/69162 "2021-10-04T02:33:46Z")\
**Posts on this page:** 6\
**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 4, 2021, 2:33am UTC](https://discourse.julialang.org/t/netcdf-file-created-in-matlab-is-read-differently-by-julia/69162/1 "2021-10-04T02:33:46Z")

</div>

I have a NetCDF file that is created using Matlab and it is composed of 'double" values of 1 and 0. I can read the values as they are without any issues inside Matlab.

However, when I tried to read it using Julia, their values all changed to 9.969209968386869e36.

Here is my code to read the nc file:

```julia

Using NCDatasets

# get the land mask (NetCDF file)
Path_mask = "xxx";
FN_mask = "test.nc";
File_mask = joinpath(Path_mask, FN_mask);

data =
Dataset(File_mask,"r") do ds0
    ds0["Land"][:,:]
end # ds is closed

```

Anyone knows what is going one?

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [October 4, 2021, 3:35am UTC](https://discourse.julialang.org/t/netcdf-file-created-in-matlab-is-read-differently-by-julia/69162/2 "2021-10-04T03:35:17Z")

</div>

Apparently `9.969209968386869e36` has been used in various places as a default “fill value” for when the application hasn’t written data. (Perhaps originating with [NCL Language Reference Guide: Variables](https://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclVariables.shtml#MissingValueAttribute) ?) So this would explain where the value is coming from, but not why reading the file gives different results from Matlab.

You could start debugging by inspecting the structure of the file using  
Matlab’s [ncdisp](https://www.mathworks.com/help/matlab/ref/ncdisp.html) and comparing to `show(io, ::NCDatasets.Dataset)`.

If you want more specific help it would be best to post a self-contained example (maybe including the example NetCDF somewhere which can be downloaded) so people can run on their own computers and reproduce the problem.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 4, 2021, 5:53am UTC](https://discourse.julialang.org/t/netcdf-file-created-in-matlab-is-read-differently-by-julia/69162/3 "2021-10-04T05:53:14Z")

</div>

Do you have the `ncdump` command-line tool? If so, have a look at the file with that.

---

<div class="post-metadata">

**Author:** ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)\
**Post date:** [October 4, 2021, 7:17am UTC](https://discourse.julialang.org/t/netcdf-file-created-in-matlab-is-read-differently-by-julia/69162/4 "2021-10-04T07:17:49Z")

</div>

Very strange. Did you make sure to close the NetCDF file inside MATLAB or is the session still running? Data might still be in cache…

---

<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 4, 2021, 11:06am UTC](https://discourse.julialang.org/t/netcdf-file-created-in-matlab-is-read-differently-by-julia/69162/5 "2021-10-04T11:06:18Z")

</div>

Many thanks for the replies, All.

Here is an example nc file: [https://leon999.s3.ca-central-1.amazonaws.com/test.nc](https://leon999.s3.ca-central-1.amazonaws.com/test.nc).

> [@c42f](#):
>
> show(io, ::NCDatasets.Dataset)

---

<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 4, 2021, 11:18am UTC](https://discourse.julialang.org/t/netcdf-file-created-in-matlab-is-read-differently-by-julia/69162/6 "2021-10-04T11:18:54Z")

</div>

Many thanks. It turns out to be the cause. After I run netcdf.close(ncid). Things are normal now.

I’m surprised to see that closing the file timely is so important.

Many thanks!
