Learn How to Edit Matlab Figures with Ease

This article aims at teaching you how to edit Matlab figures, a critical skill in your toolkit as a Python engineer or computer vision expert. With deep knowledge of Matlab, Python, and Computer Visio …

Updated November 4, 2023


Hey! If you love Computer Vision and OpenCV as much as I do let's connect on Twitter or LinkedIn. I talk about this stuff all the time and build cool projects.


This article aims at teaching you how to edit Matlab figures, a critical skill in your toolkit as a Python engineer or computer vision expert. With deep knowledge of Matlab, Python, and Computer Vision, we’ll delve into this topic in great detail, including how it is used and provide clear explanations with practical code samples.

Introducing Matlab Figure Editing

Matlab, a well-known programming environment developed by MathWorks, is widely used for various engineering applications like signal processing, machine learning, and computer vision. It’s essential to know how to edit figures in Matlab when working on projects involving complex datasets and visualizations. In this article, we will focus on the process of editing these graphs effectively with your preferred programming language, Python.

Editing Matlab Figures from Python

Using Python, it’s possible to control and modify existing Matlab figures, making it a valuable tool for Python engineers and computer vision experts who need to make changes to their plots created in Matlab. We can achieve this by employing Matplotlib, a comprehensive library used for plotting and visualization of data in Python that is compatible with many other scientific packages such as NumPy, SciPy, and Pandas.

Prerequisites

Before diving into the editing process, make sure you have:

  1. A working knowledge of both Matlab and Python programming languages
  2. The latest version of Matplotlib installed in your Python environment (you can install it using pip install matplotlib).
  3. The figure to be edited must already exist within the Matlab workspace as a variable, e.g., figure_to_edit.
  4. Have access to both Matlab and Python IDEs for ease of working between the two environments.

Connecting Python to Your Matlab Environment

To edit an existing figure from within a Python program, you first need to establish a connection with the running Matlab environment. For this, you can use a tool called PyLab, which provides bidirectional communication between Matlab and Python by allowing Python scripts to control variables in your Matlab workspace. To do so, follow these steps:

  1. Launch both Python and Matlab environments simultaneously.
  2. Import the necessary libraries in the Python environment, such as numpy, matplotlib, and pylab.
  3. Initialize the connection between Python and Matlab using pylab.connect() from within your Python program.
  4. Specify the location of the executable for both Python and Matlab to ensure they can successfully communicate, e.g., pylab.path(r'C:\Program Files\MATLAB\R2018b').
  5. Finally, establish a connection with the Matlab environment by running the Python script containing the import and initialization statements from within the Python IDE (e.g., Spyder or Jupyter Notebook). This will launch both environments simultaneously and allow you to connect to your Matlab workspace variables.

Controlling Matlab Variables in Python

Now that you have established a connection between Python and Matlab, let’s learn how to access and modify the figure-related data within your running Matlab environment from within your Python program.

  1. Accessing Matlab Data: To access variables or objects stored in the Matlab workspace, use the pylab.getvariable() function. For instance, you can retrieve the figure_to_edit variable by running var = pylab.getvariable('figure_to_edit').
  2. Modifying Matlab Data: You can also manipulate variables stored within your Matlab environment. Say, if you want to change some property of the figure-related data (e.g., modifying the color scheme or adding labels), assign the new value to the variable using Python’s syntax. For example: var.Property = 'NewValue'.
  3. Updating Variables in Matlab: To save these changes back into your Matlab environment, use the pylab.putvariable() function. This will update the specified variable within the connected Matlab workspace with the new values provided from Python. For example, if you made modifications to var, run pylab.putvariable('figure_to_edit', var).
  4. Saving Changes in Matlab: Once your changes have been incorporated into the figure using Python and saved in the Matlab environment using pylab.putvariable(), any further changes made within Matlab will be retained when you reconnect with Python next time, preserving the modifications across both environments seamlessly.

Conclusion

In this comprehensive guide on editing Matlab figures from a Python environment, we have covered how to establish a connection between the two programming languages through the PyLab tool, allowing for a smooth exchange of data and manipulation of variables. By utilizing the power of both Python and Matlab, you can now effectively edit your plots created in Matlab using Python’s rich visualization library: Matplotlib. As a Python engineer or computer vision expert, this knowledge will undoubtedly enhance your skillset and contribute to your success in future projects that combine various programming languages for data analysis, visualization, and more.


Stay up to date on the latest in Computer Vision and AI

Intuit Mailchimp