Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we’re going to explore how to execute MATLAB scripts using Python. Can anyone tell me what a .m file is?
Isn’t it a script written in MATLAB?
Exactly! .m files are MATLAB script files. In Python, we can run these scripts using the MATLAB Engine API. So, how do we start this API in Python?
We need to import matlab.engine?
Right! After that, we initiate the MATLAB engine with eng = matlab.engine.start_matlab(). This allows us to communicate with the MATLAB session.
What command do we use to run a script?
Good question! We use eng.run('myscript.m', nargout=0). The nargout=0 indicates that we don't expect any output back from the script.
Can we pass parameters into that script?
Yes! We can set variables in the MATLAB workspace from Python, like eng.workspace['x'] = 42, so the script can use it. This is pretty powerful!
To summarize, we import the MATLAB engine, start it, run our scripts, and pass parameters directly into MATLAB’s workspace.
Now that we know how to run scripts, let’s discuss how we can retrieve results after execution. What's an example of getting back a variable from MATLAB?
Would it be something like result = eng.workspace['y']?
Perfect! This line retrieves the variable 'y' from the MATLAB workspace back into Python. This allows us to use MATLAB’s calculations in our Python environment.
What kind of data can we get back?
Great follow-up! You can retrieve various data types, such as numbers, arrays, and even strings. Just be mindful of the data type conversions between Python and MATLAB!
Can you give us an example of all this?
Sure! For instance, if you modify y based on some input x in your MATLAB script, after execution, you might access result in Python and use it for further analysis.
In summary, we can pass parameters into our scripts, run them, and extract results back into our Python code efficiently.
Let’s look at a simple implementation. Imagine you have a MATLAB script that computes the sum of an array. How would we set that up in Python?
We start with importing the MATLAB engine and then run the script?
Correct! And we’d also need to prepare the input array in Python, converting it to the appropriate MATLAB type first.
How do we convert it to MATLAB type?
You would use matlab.double() to convert a Python list, for example. This handles the data type difference.
And after we run the script, we can retrieve the sum in Python?
Yes! Once your script finishes running, you'll access the output variable to retrieve the result. Always ensure your script outputs as expected, and you check the types when extracting!
As a summary, always convert your inputs correctly, run the script, and extract your results into Python for further use.
Run your script with eng.run, it's really easy and a lot of fun!
Imagine Python as an assistant who fetches and executes tasks given by MATLAB, waiting eagerly for results to come back.
Remember RRP: Run it, Retrieve it, Pass parameters.
PIG: Python Interacting with MATLAB via the MATLAB Engine.
Term
How to start the MATLAB engine in Python?
Click to flip
Term
What command retrieves variable 'y' from MATLAB workspace?
Click to flip
Term
What is nargout?
Click to flip
A script written for MATLAB, with a .m file extension, used for numerical computations.
An interface that allows Python scripts to interact with a MATLAB session.
A parameter used in MATLAB to specify the number of output arguments to return.
The environment in MATLAB where variables are stored and can be accessed during operations.
This section explains how to execute MATLAB scripts in Python, allowing parameter passing and workspace interaction.
In this section, you'll learn about how to run MATLAB .m scripts directly from Python using the MATLAB Engine API. It covers key commands for executing scripts, passing parameters, and retrieving results from the MATLAB workspace.
In this section, we explore how to effectively execute MATLAB scripts (.m files) from within Python using the MATLAB Engine API, which provides seamless interaction between the two programming environments. The main command used for running a MATLAB script is eng.run('myscript.m', nargout=0), where eng is the MATLAB engine instance previously started. Additionally, you can pass parameters into the MATLAB workspace using commands like eng.workspace['x'] = 42 to set variables accessed by the script. After executing the script, results can be extracted back to Python using result = eng.workspace['y']. This integration allows users to take advantage of MATLAB's powerful numerical capabilities while leveraging Python's flexibility, ultimately enhancing productivity in scientific computing.