3

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

1 Answer 1

7

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')
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, your solution worked. I can't believe that the issue was so easily fixed and that the address didn't work even though I copied the address directly from the windows explorer address bar
@kynnem For sure! I know of this error because this has happened to me before. Actually if you don't double the backslashes, the error is undefined because you are specifying the actual path to the file. On Python 3, I get a unicode error. On Python 2, I get an invalid mode error. To be platform agnostic, consider using os.path.join. That way it correctly inserts the directory delineator character(s) regardless of the operating system: data = sio.loadmat(os.path.join('C:', 'Users', 'Martin', ...))
I generally find pathlib nicer to use than os.path.
@excaza interesting. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.