Access specified columns from .mat file in python

I have a total of 61225 matrices each having the same variable names with different data. I am interested only in the file having variable name Pose_Para this is a 1x7 file out of which I am only interested in the 2nd,3rd and 4th column. My problem is how do I access these columns only so that I obtain a matrix with size [61225 x 3]?

Here is what I have done till now.

import glob
from import loadmat
entries = glob.glob('300W_LP/*/*.mat', recursive=True)
for entry in entries:
    para = loadmat(entry, variable_names = ("Pose_Para",))["Pose_Para"]


I’m presuming that para = loadmat(entry, variable_names = ("Pose_Para",))["Pose_Para"] results in para being a 7-element numpy array. If that’s the case, you can do:

import numpy as np

# ...file handling...
allparas = np.empty((len(entries),3))
for ctr,entry in enumerate(entries):
    allparas[ctr,:] = loadmat(entry, variable_names = ("Pose_Para",))["Pose_Para"][1:3]

Note that you might need to add squeeze_me = True to your loadmat call. I’m not sure if loadmat needs this to yield a 7-element 1D Numpy array as opposed to a 1×7 2D array.

You could also use a list comprehension to write the for loop as a one-liner:

allparas = np.array([loadmat(entry, variable_names= ("Pose_Para",))["Pose_Para"][1:3] for entry in entries])

Leave a Reply

Your email address will not be published. Required fields are marked *