0

I’m trying to read a subfolder from .mat file in Python using hdf5 package. I could extract the integers and values from the datase, but couldn’t read the strings. result=matfile[“/pro/tttt/rrr”]

Return is a series of lists:

[[<HDF5 object reference>] [<HDF5 object reference>] [<HDF5 object reference>]]

I need to extract these strings and combine them with another column as its header. Any suggestions?

1 Answer 1

0

An object reference points to another object (typically another dataset) in the file. You use it to get the referenced object. There are several SO answers with examples. Use this search to find them "[matlab] [h5py] object". Here is an answer I wrote earlier this year that is a good starting point: reading matlab data into numpy arrays. Here is a link to related h5py doc: Object and Region References.

I wrote a short example for your file. See code below. It gets the dataset referenced by the first object. Do the same for [1], [2], etc.
array_from_objref = matfile[ matfile[“/pro/tttt/rrr"][0] ][:].
Print the full path with this:
print(matfile[“/pro/tttt/rrr"][0].name)

Sign up to request clarification or add additional context in comments.

8 Comments

I tried array_from_objref = file[ file['/pro/tttt/rrr'][0] ][:] then I got the following error: AttributeError: 'numpy.ndarray' object has no attribute 'encode' do you know why? but it's true, those string are a reference (it's the header) for another array in that dataset
What do you get when you print the path name? The reference to encode suggests this is string/character data. That can be more complicated. Check the dtype with print(matfile[“/pro/tttt/rrr"][0].dtype)
It’s an object!
Sorry, I meant print( matfile[ matfile[“/pro/tttt/rrr"][0] ].dtype ) to get the dtype of the referenced object. Or, if you print the name, you can use that instead.
just a heads up that I get it to a point now printing the characters one by one! LoL : >>> element=file[file["/pro/tttt/rrrrr"][0][0]] >>> elementStr=''.join(chr(i) for i in element[0]) >>> print(elementStr) S , if I run with [1], it prints the second character, etc,. not working with [:] neighter len(x) :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.