Reading Matlab files – Structures

Suppose you have saved a structure called “data” in matlab:

% "x" and "y" are two fields in the structure "data"
data.x = linspace(1,100,100);
data.y = linspace(1,100,100);

% save a .mat file

To read “x” and “y” into python:


# import file into a dictionary
f ='matlabfile.mat')

# read in the structure
data = f['data']

# get the fields
x = data[0,0]['x']
y = data[0,0]['y']

3 thoughts on “Reading Matlab files – Structures

  1. Hi ,i have cjdata struct in data.mat. I want to access cjdata structure fields in python.struct field are stored as cjdata.image , etc.How can I do this.
    import as io
    fname = ‘C:/Users/join2/Desktop/FIGSHARE/NewSingle/data.mat’;
    data= io.loadmat(fname)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s