Skip to content

EOFError when reading valid MATLAB v4 file with zero-sized dimension (Nx0 matrix) #240

@drbergman

Description

@drbergman

Hi,

I ran into an EOFError when reading a MATLAB v4 .mat file that appears to be valid but contains a zero-sized dimension.

Summary

Calling matread on a v4 MAT file with shape (N, 0) results in:

ERROR: EOFError: read end of file

Reproduction

The file is identified as:

Matlab v4 mat-file (little endian) cells, numeric, rows 88, columns 0

A minimal inspection of the header confirms:

  • rows = 88
  • columns = 0
  • variable name = "cells"

No data payload follows (as expected since 88×0 = 0 elements).

Observed behavior

matread(file) throws:

EOFError: read end of file

Stacktrace points to:
MAT_v4.read_matrix → read_header → read(Int32)

julia> A = matread(mat_file)
ERROR: EOFError: read end of file
Stacktrace:
 [1] read(s::IOStream, T::Type{Int32})
   @ Base ./iostream.jl:437
 [2] read_bswap
   @ ~/.julia/packages/MAT/utCif/src/MAT_v4.jl:66 [inlined]
 [3] read_header(f::IOStream, swap_bytes::Bool)
   @ MAT.MAT_v4 ~/.julia/packages/MAT/utCif/src/MAT_v4.jl:113
 [4] read_matrix(f::IOStream, swap_bytes::Bool)
   @ MAT.MAT_v4 ~/.julia/packages/MAT/utCif/src/MAT_v4.jl:123
 [5] read(matfile::MAT.MAT_v4.Matlabv4File)
   @ MAT.MAT_v4 ~/.julia/packages/MAT/utCif/src/MAT_v4.jl:174
 [6] matread(filename::String; table::Type, convert_opaque::Bool)
   @ MAT ~/.julia/packages/MAT/utCif/src/MAT.jl:192
 [7] matread(filename::String)
   @ MAT ~/.julia/packages/MAT/utCif/src/MAT.jl:188
 [8] top-level scope
   @ REPL[21]:1

Expected behavior

Since zero-sized matrices are valid in MATLAB semantics, I would expect one of:

  1. Return an empty Julia array of size (88, 0), or
  2. Gracefully handle and skip the variable, or
  3. Throw a more informative error indicating unsupported zero-sized dimensions

Notes

This appears to arise because the v4 reader attempts to read additional data even when the computed payload size is zero.

Workaround

Currently handling this on the user side by catching EOFError or pre-checking file size.

Question

Would you consider adding a guard in the v4 reader for zero-sized matrices to avoid attempting further reads?

Happy to help test a patch if useful.

Note: Text written largely by ChatGPT. I manually checked these conclusions and can confirm to the best of my ability that this is the true problem. I do not have an active MATLAB license, so I cannot test there easily.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions