Search code examples
pythonmatlabioscipy

Error when opening .mat file in python


I'm trying to open a MATLAB file which is an 'array of structures'. When using scipy.io.loadmat to open the file, I get the following error:

File "<ipython-input-15-0951b80baef6>", line 1, in <module>
    data = sio.loadmat('C:\Users\Martin\Desktop\Biophysics PhD\Results\180321_agonists_spreading_conditions\180321_agonists_spreading_conditions\Compare_ADPdexBSA.mat')

File "C:\Users\Martin\Anaconda2\lib\site-packages\scipy\io\matlab\mio.py", line 141, in loadmat
MR, file_opened = mat_reader_factory(file_name, appendmat, **kwargs)

File "C:\Users\Martin\Anaconda2\lib\site-packages\scipy\io\matlab\mio.py", line 64, in mat_reader_factory
byte_stream, file_opened = _open_file(file_name, appendmat)

TypeError: 'NoneType' object is not iterable

Still new enough to programming, so I'm not sure how to interpret the error. Any help you can give me is greatly appreciated


Solution

  • This error is most likely happening because scipy.io.loadmat cannot find the file of interest. Because you're using Windows, the path you're defining is not quite correct. You need to delineate the directory separator \ with two backslashes: \\.

    In other words:

    data = sio.loadmat('C:\\Users\\Martin\\Desktop\\Biophysics PhD\\Results\\180321_agonists_spreading_conditions\\180321_agonists_spreading_conditions\\Compare_ADPdexBSA.mat')