How to run .m file in python?

X
Xinfeng Li · Jan 15, 2025 · 652 views
Question
I have a .m code what I want to run in python. Is it any easy way? 1. this code is not a function. 2. don't want to show the matlab window.  
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 12, 2026
This is not a big deal. The python code looks like:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.simple_script(nargout=0)
eng.quit()

The Matlab script would be perhaps this one line saved as simple_script.m:

a = 'it works easily...'
Make sure that the script is saved in a folder matlab knows as a search folder.
Then run your python script and get the answer:
a =
    'it works easily...'
It is not necessary that you have a function in your .m-file, but possible. See more information here.
The Matlab screen is not visible by default, so you should be happy if you follow this and the linked information...
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!