Python read matlab m file

(converting my comments into an answer)

A .m file is a matlab / octave script. You don't "load" a script. Not even in matlab / octave. You can only run it, and this may or may not result in a particular workspace that you can then save into a .mat file.

There are "matlab interfaces" for python that you could use to run an .m file from within a python session, e.g. the official MATLAB Engine API for Python, or if you have an older matlab version you could try finding other python matlab interfaces online (a simple google search gave me pymatlab, mlab, mlabwrap etc). If your .m file is octave-compatible (and you have octave installed) you could also try the oct2py interface which is known to work well. Once you have one of those interfaces installed, you could either attempt to run the script and then transfer the workspace variables onto python, or save to a .mat file using the interface, and then load it back onto python using scipy.io.loadmat as normal.

As for the .mat file giving you an error, the most likely reason for this is that it's a newer version of the .mat specification, which python doesn't recognise yet (what matlab version are you using?). Try loading the .mat file in matlab (or octave) and saving it again with the -v7 option. Python should then open this without problems.

EDIT: Here's an example running an octave script via oct2py, saving the resulting octave workspace onto a .mat file, and loading the .mat file into python.

%% file myscript.m located at /home/tasos/Desktop
a = 1
b = 2
c = 3
# from python:
from oct2py import octave as oct
oct.eval("cd /home/tasos/Desktop")
oct.eval("myscript")
oct.eval("save -v7 myworkspace.mat")

from scipy.io import loadmat
D = loadmat("/home/tasos/Desktop/myworkspace.mat")
print D.keys() 
#>>> ['a', 'c', 'b', '__header__', '__globals__', 'ans', '__version__']
print D['a'], D['b'], D['c']
#>>> [[ 1.]] [[ 2.]] [[ 3.]]

How do I read a .m file in Python?

Use the scipy.io Module to Read .mat Files in Python..
Use the NumPy Module to Read mat Files in Python..
Use the mat4py Module to Read mat Files in Python..
Use the matlab.engine Module to Read mat Files in Python..

How do I call a MATLAB file in Python?

There are two approaches for calling MATLAB code from Python. The first is to use the MATLAB Engine API for Python, which requires a MATLAB install. The second is to use MATLAB Compiler SDK to compile a Python package that does not require users to have a MATLAB install. Let's first see our MATLAB code.

How do I read .m files?

If a text editor doesn't display the file correctly, try MATLAB or Mathematica. Text-based M files can be converted to other text formats with those same programs.

How do I open a .MAT file without MATLAB?

It is not possible to open it with a text editor (except you have a special plugin as Dennis Jaheruddin says). Otherwise you will have to convert it into a text file (csv for example) with a script. This could be done by python for example: Read . mat files in Python.