Skip to content

fix: fix bug where multiple fields' structures can only read the first field with mat73 - #321

Open
usopp333 wants to merge 1 commit into
tbeu:masterfrom
usopp333:FIX/ReadMultiFieldsStruct
Open

fix: fix bug where multiple fields' structures can only read the first field with mat73#321
usopp333 wants to merge 1 commit into
tbeu:masterfrom
usopp333:FIX/ReadMultiFieldsStruct

Conversation

@usopp333

Copy link
Copy Markdown
s.a = [1, 2, 3,4]; s.b=[2,3,4,5];
save('s.mat', 's');
mat_t* matfp = Mat_Open(filename, MAT_ACC_RDONLY);
matvar_t* matvar = Mat_VarReadNext(matfp);
unsigned fields_num = Mat_VarGetNumberOfFields(matvar); // fields_num == 1
  • mat73: s.a = [1, 2, 3,4]; s.b=[2,3,4,5]; save('s.mat', 's'); only read s.a;
  • mat5: no bug
  • fix bug by Cursor-AI
  • Update H5Literate return value handling in Mat_H5ReadGroupInfo and Mat_H5ReadGroupInfoIterate functions

@usopp333
usopp333 force-pushed the FIX/ReadMultiFieldsStruct branch from d3e0fbd to 64d0f52 Compare July 15, 2026 03:18
@tbeu

tbeu commented Aug 3, 2026

Copy link
Copy Markdown
Owner

Which tool and version did you use to save the MAT-file? I cannot reproduce the reported issue with the snippet above, because MATLAB R2026a saves a struct's field names in a MATLAB_fields attribute on the struct group. When that attribute is present, matio reads the fields from it (Mat_H5ReadFieldNames), so the H5Literate loop this patch modifies is never executed. The bug therefore only triggers on files without MATLAB_fields, e.g. from other HDF5-based writers.

Reproduction: the issue appears as soon as the attribute is removed. matdump on the MATLAB-saved file reads both fields correctly:

> matdump -d issue321_v73.mat
      Name: s
      Rank: 2
Class Type: Structure
Fields[2] {
1 2 3 4
2 3 4 5
}

After deleting MATLAB_fields from /s, only the first field is read:

> matdump -d issue321_v73_nofields.mat
      Name: s
      Rank: 2
Class Type: Structure
Fields[1] {
1 2 3 4
}

h5dump confirms the two files differ only by that attribute:

> h5dump -n 1 -H issue321_v73.mat
FILE_CONTENTS {
 group      /
 group      /#refs#
 dataset    /#refs#/a
 attribute  /#refs#/a/MATLAB_class
 attribute  /#refs#/a/MATLAB_empty
 group      /s
 attribute  /s/MATLAB_class
 attribute  /s/MATLAB_fields
 dataset    /s/a
 attribute  /s/a/H5PATH
 attribute  /s/a/MATLAB_class
 dataset    /s/b
 attribute  /s/b/H5PATH
 attribute  /s/b/MATLAB_class
 }
> h5dump -n 1 -H issue321_v73_nofields.mat
FILE_CONTENTS {
 group      /
 group      /#refs#
 dataset    /#refs#/a
 attribute  /#refs#/a/MATLAB_class
 attribute  /#refs#/a/MATLAB_empty
 group      /s
 attribute  /s/MATLAB_class
 dataset    /s/a
 attribute  /s/a/H5PATH
 attribute  /s/a/MATLAB_class
 dataset    /s/b
 attribute  /s/b/H5PATH
 attribute  /s/b/MATLAB_class
 }

Could you provide the actual .mat file from your report (or the MATLAB version used)? Note that a regression test would need a v7.3 struct file without MATLAB_fields, since MATLAB-generated files never exercise this code path.

issue321_test_files.zip

@tbeu
tbeu force-pushed the FIX/ReadMultiFieldsStruct branch from 64d0f52 to 3c0fe3e Compare August 8, 2026 18:53
…t field with mat73

- mat73: s.a = [1, 2, 3,4]; s.b=[2,3,4,5]; save('s.mat', 's'); only read s.a;
- mat5: no bug
- fix bug by Cursor-AI
- Update H5Literate return value handling in Mat_H5ReadGroupInfo and Mat_H5ReadGroupInfoIterate functions
@tbeu
tbeu force-pushed the FIX/ReadMultiFieldsStruct branch from 3c0fe3e to a2430ba Compare August 11, 2026 20:07
@tbeu

tbeu commented Aug 11, 2026

Copy link
Copy Markdown
Owner

@usopp333 Any feedback?

@usopp333

Copy link
Copy Markdown
Author

Structs.zip

image

Lum_struct load failed

  • Lum_struct version=512 is MAT_FT_MAT73
  • Sim_struct version=512 is MAT_FT_MAT73

Matlab 2025b

image

vscode debugger

  • Lum_struct Failed
image - Sim_Struct image

The version of MATLAB I am using is 2025b

@tbeu

tbeu commented Aug 13, 2026

Copy link
Copy Markdown
Owner

But which tools created Lum_struct.mat. I doubt it was MATLAB R2025b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants