How to Run M Files in Python: A Comprehensive Guide

Python is an open-source, high-level programming language that has a wide range of applications in various domains. It is a versatile language that can be used for developing applications, web development, scientific computing, and more. One of the significant advantages of Python is its extensibility. It allows users to integrate code written in other languages such as C, C++, and Fortran. M files, which are typically used in MATLAB, are an example of such code that can be integrated with Python. In this article, we will explore how to run M files in Python.

Table of Contents

What are M files?

M files are a type of file that is used in MATLAB, a proprietary programming language and development environment widely used in engineering, science, and mathematics. The M files contain MATLAB code that can be executed within the MATLAB environment. These files have a .m extension and can be created using any text editor.

M files are commonly used for numerical analysis, data preprocessing, and data visualization. They contain functions, scripts, and commands that can be executed in MATLAB’s interactive command window or as a part of a larger program.

Why run M files in Python?

Python is a popular programming language that has a vast ecosystem of libraries and tools. It has become the de facto language for scientific computing and data analysis. Many scientific libraries such as NumPy, SciPy, and Matplotlib are written in Python.

Running M files in Python allows users to take advantage of the Python ecosystem while reusing existing MATLAB code. It also enables users to combine the strengths of both languages. For example, users can use Python libraries such as TensorFlow for machine learning and Deep Learning while leveraging the power of MATLAB for numerical analysis and data preprocessing.

Installing required dependencies

Before we can run M files in Python, we need to install the necessary dependencies. The following are the dependencies required to run M files in Python:

  1. MATLAB Engine API for Python
  2. NumPy
  3. SciPy

The MATLAB Engine API for Python is required to interface with MATLAB from Python. NumPy and SciPy are Python libraries that are commonly used for scientific computing.

To install the MATLAB Engine API for Python, we need to have MATLAB installed on our system. We can then install the MATLAB Engine API for Python by running the following command in the command prompt:

python -m pip install matlab.engine

To install NumPy and SciPy, we can use the following command:

python -m pip install numpy scipy

Running M files in Python

Once we have installed the necessary dependencies, we can run M files in Python by following the steps below:

  1. Start MATLAB and open the M file that we want to run.
  2. Make sure that the M file is in the MATLAB path.
  3. In Python, import the MATLAB Engine API by running the following code:
import matlab.engine
  1. Start a MATLAB engine session by running the following code:
eng = matlab.engine.start_matlab()
  1. Call the function in the M file by running the following code:

The function_name is the name of the function in the M file, and the arguments are the input arguments to the function. The output of the function can be assigned to a variable in Python.

For example, let’s say we have an M file that contains a function called add_numbers(a, b) that adds two numbers and returns the result. We can call this function from Python by running the following code:

import matlab.engine

eng = matlab.engine.start_matlab()

result = eng.add_numbers(2, 3)


This code will output 5, which is the result of the add_numbers function.

Debugging M files in Python

Debugging M files in Python can be challenging, as we need to debug the code in both MATLAB and Python. However, there are a few tips that can help us debug our code effectively.

  1. Use the disp function in MATLAB to print debug messages.
  2. Use the fprintf function in MATLAB to write debug messages to a file.
  3. Use the matlab.engine.traceback() function in Python to print the MATLAB stack trace.

By using these techniques, we can effectively debug our M files in Python.


Running M files in Python can be a powerful tool in our arsenal. It allows us to leverage the strengths of both MATLAB and Python while taking advantage of the vast ecosystem of Python libraries and tools. In this article, we have seen how to run M files in Python, install the necessary dependencies, and debug our code effectively. By following these steps, we can unlock the full potential of our MATLAB code in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *