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
save('matlabfile.mat','data')

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

import scipy.io

# import file into a dictionary
f = scipi.io.loadmat('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 ,cjdata.id etc.How can I do this.
    import scipy.io as io
    fname = ‘C:/Users/join2/Desktop/FIGSHARE/NewSingle/data.mat’;
    data= io.loadmat(fname)
    cjdata=data[‘cjdata’]

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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