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'll explore the MATLAB Engine API for Python. This API allows us to start MATLAB from Python and use MATLAB's powerful computational engines directly in our Python environment.
How do we start using the MATLAB Engine API?
Great question! First, we need to install it. We navigate to the 'matlabroot/extern/engines/python' folder in our command line and execute python setup.py install.
What does 'matlabroot' refer to?
Good inquiry! 'matlabroot' is the directory where MATLAB is installed, which includes various toolboxes and APIs. Now, let’s see how we can start a MATLAB session from Python.
What would a simple command look like for starting MATLAB?
You would use import matlab.engine and then eng = matlab.engine.start_matlab(). Remember this as a key format: import followed by start_matlab().
Got it! Can you summarize this session?
Sure! We discussed the MATLAB Engine API, installation steps, and basic commands to start MATLAB from Python. Always remember to locate MATLAB's installation directory for proper setup.
Now let’s dive into how we can call MATLAB functions from Python and the importance of data type conversion.
What are the main data types we need to take care of?
MATLAB supports several data types like basic numeric types, arrays such as matlab.double, and strings. We must convert our Python data into these formats.
Can you show us a code example?
Yes! Let’s create a 2D array with a = matlab.double([[1, 2, 3], [4, 5, 6]]), then to sum it in MATLAB, you would call b = eng.sum(a, 1). Can you repeat that structure?
Sure! It’s matlab.double for arrays and then use eng.function_name().
Exactly! Always remember, converting data types is crucial to avoid errors when calling functions.
Lastly, let’s discuss how we can execute MATLAB scripts directly from Python.
What about passing parameters to MATLAB scripts?
Excellent point! You can pass parameters to the MATLAB workspace using commands like eng.workspace['x'] = 42. This assigns the value 42 to a variable x in MATLAB.
What's the next step after assigning a variable?
You can then execute your script with eng.run('myscript.m', nargout=0). nargout=0 means you’re not expecting any output back. Can anyone tell me why this is important?
It reduces memory usage if we aren't returning values.
Correct! Finally, you can retrieve values from MATLAB’s workspace using something like result = eng.workspace['y'].
The process of allowing different programming languages to work together, notably Python and MATLAB in this context.
The steps needed to set up the MATLAB Engine API for use within Python.
Techniques for invoking MATLAB functions and handling data types.
Running MATLAB scripts from Python and managing workspace variables.
To run MATLAB from Python, it’s quite sublime, just import and start, it won't waste your time!
Imagine a scientist who uses Python for data but has MATLAB for heavy calculations. They get the best of both worlds by using the MATLAB Engine API – bridging their two favorite tools seamlessly!
To remember the process of calling functions: 'FADS' - Functions, Arrays, Data types, Scripts.
MAPI - MATLAB API for Python Integration.
Term
How to start a MATLAB session from Python?
Click to flip
Term
What does nargout specify?
Click to flip
Term
How do you assign a variable in MATLAB workspace from Python?
Click to flip
An interface that allows Python programs to call MATLAB functions and execute MATLAB scripts.
The process of converting data types from Python formats to MATLAB formats and vice versa for compatibility.
The environment in which variables are stored and can be manipulated in MATLAB.
This section outlines the basics of integrating Python with MATLAB, including the use of the MATLAB Engine API for Python and fundamental methods for calling MATLAB functions and executing scripts.
The integration of Python with MATLAB is facilitated through the MATLAB Engine API for Python, enabling users to interact seamlessly between the two languages. This section discusses installation, basic usage, and methods for data type conversion, exemplified by calling MATLAB functions and executing MATLAB scripts directly from Python.
This section focuses on the foundational techniques for integrating Python with MATLAB, leveraging the MATLAB Engine API. It specifies how to set up the integration by installing the engine API and showcases its capabilities in bridging Python scripts with MATLAB functionalities.
MATLAB Engine API for Python: MATLAB offers an official API allowing Python scripts to initiate and interact with a MATLAB session. Installation involves navigating to the appropriate directory and running python setup.py install.
Basic Usage in Python: Users can import matlab.engine, start a MATLAB session, and directly call MATLAB functions, as demonstrated by calculating the square root of 16 in MATLAB through Python.
Calling MATLAB Functions: Data types between Python and MATLAB must be convertible for efficient communication. The MATLAB engine supports various data types including basic numeric types, arrays (matlab.double, matlab.int32), and strings.
MATLAB Example: The section illustrates a simple example where a 2D array is created in Python and summed in MATLAB to demonstrate data interaction.
Executing MATLAB Scripts: MATLAB .m scripts can be executed within Python, allowing dynamic parameter passing and data retrieval using the MATLAB workspace.
Overall, this section serves as a vital introduction to employing MATLAB's numerical capabilities within a Python environment, enhancing scientific computing workflows.