question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

hd5f to access to nested structures in *.mat files

See original GitHub issue

General information

  • Operating System (e.g. Windows 10, MacOS 10.11, Ubuntu 16.04.2 LTS, CentOS 7): Windows 10
  • Python version (e.g. 2.7, 3.5): 3.7.3
  • Where Python was acquired (e.g. system Python on MacOS or Linux, Anaconda on Windows): Anaconda
  • h5py version (e.g. 2.6): 2.9
  • HDF5 version (e.g. 1.8.17) 1.10.4
  • The full traceback/stack trace shown (if it appears)

I getting access to the following structure of matlab file (*.mat): image

which contains the following set of structures:

image

I try to access to the fields inside each structure using hd5f python lib

image

using the following code:

mat_dict = h5py.File(file_path, 'r')
result = mat_dict['JKPRR']['results']['ChangingKnob'][0]['result']

But apparently I can’t go deep from:

result = mat_dict['JKPRR']['results']['ChangingKnob'][0]

Do you know how I can go to the deeper fields?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
takluyvercommented, Sep 16, 2020

First, the easy bit: it looks like the ChangingKnob dataset is 2D, so to get one item out, you need two indices:

mat_dict['JKPRR']['results']['ChangingKnob'][0, 0]

# This is equivalent but a bit easier to read:
mat_dict['JKPRR/results/ChangingKnob'][0, 0]

It looks like Matlab is storing HDF5 references (a kind of link) in the dataset, so then you need to follow the reference:

ref = mat_dict['JKPRR/results/ChangingKnob'][0, 0]
dataset = mat_dict[ref]

Hopefully after that you can do dataset['result'] to access one field, unless Matlab has made more obstacles.

0reactions
takluyvercommented, Sep 18, 2020

Closing this as it looks (from the 👍) like it’s resolved. We can continue the discussion or reopen it if needed.

To summarise for other people looking at this: Matlab’s .mat files do appear to be HDF5, but they might be oddly structured and require some fiddling to work out how to get the data you want. You may want to use a HDF viewer tool like HDFview or h5glance to inspect it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use hd5f to access to nested structures in *.mat files?
Solution & discussion in following issue: https://github.com/h5py/h5py/issues/1668. First, the easy bit: it looks like the ChangingKnob ...
Read more >
How to use hd5f to access to nested structures in *.mat files?
I try to get access to the following structure of matlab file (*.mat):. parent matlab structure. which contains the following set of structures....
Read more >
Access Data in Nested Structures - MATLAB & Simulink
This example shows how to index into a structure that is nested within another structure. The general syntax for accessing data in a...
Read more >
How to use HDF5 files in Python
Each HDF5 file has an internal structure that allows you to search for a specific dataset. You can think of it as a...
Read more >
Read complex .mat files - PyHOGS
The function below loads a . mat file and converts the nested scalar structures into nested dictionaries. If you aren't using arrays of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found