Category Archives: MATLAB

Set types of properties in MATLAB classes

I got an error when I tried to initialize a property of a class to another MATLAB class. The error read:

Conversion to double from (Class Name) is not possible.

So I looked for a way to specify the type of a property and found the link above. In short, @Type needs to follow the name of a property like

classdef (Class Name)

Or property Type is also possible since MATLAB R2016a.

Reading HDF5 in MATLAB

In Reading a mat file into Python, I learned how to read a mat file for Python programs. What about the reverse? In MATLAB, the relevant functions are hdf5info and hdf5read.

First, we need to save data in Python by using h5py:

import numpy as np
import h5py

f = h5py.File('FileName', 'w')

dataset = f.create_dataset('VariableName', data = numpyArrayContainingData)


You may check the h5py document regarding the method create_dataset.

Then, try the following in MATLAB:

hdf5read('FileName', 'VariableName')

Reading a mat file into Python

According to a StackOverflow post, one can use scipy to read a matlab file, but it does not support the latest format Version 7.3 and later. Instead, one can use h5py since the format Version 7.3 and later follows HDF5.

Then, I looked for a solution to read a matlab file by using h5py and found another StackOverflow post, which describes how to extract the names of variables and their values. Since I know that Panda‘s DataFrame is good to deal with labelled datasets, I dug Pandas documents. What is possible is to convert a dict to Pandas Series. I think it is because the array for each label can be multi-dimensional.

import numpy as np
import pandas as pd
import h5py

f = h5py.File('FileName','r')

dataset = dict()

for name, data in f.items():
    if type(data) is h5py.Dataset:
        dataset[name] = data.value

series = pd.Series(dataset)

So one can address the content of a variable x by series['x'].