Plot Excel Data in Matlab - A Comprehensive Guide with Code Samples

In this article, we will delve into the fascinating world of data visualization by learning how to plot Excel data in Matlab. We will explore essential topics such as importing and reading Excel files …

Updated October 2, 2023


Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

In this article, we will delve into the fascinating world of data visualization by learning how to plot Excel data in Matlab. We will explore essential topics such as importing and reading Excel files, utilizing Matlab tools for manipulating and analyzing data, creating visually appealing plots using various chart types, and implementing best practices for efficient data representation. By the end of this tutorial, you’ll have a strong understanding of how to leverage Matlab’s extensive capabilities for data analysis, making it an invaluable skill for any Python Engineer or Computer Vision Expert.

Introduction

Data visualization is a critical aspect of any programming language and software suite, as it helps to communicate complex ideas and trends with ease. Matlab, developed by MathWorks, is known for its extensive support for data analysis, processing, modeling, and visualization in engineering, scientific, and technical applications. In this article, we will focus on leveraging Matlab’s powerful tools to plot Excel data, making it a valuable resource when working with spreadsheet data in various domains.

Importing Excel Data into Matlab

Before diving into the visualization process, we need to import the desired Excel file or worksheet into our Matlab workspace. There are multiple ways to achieve this, and the most common approach involves using the “readmat” command to read a MAT-file (Matlab data format) generated by exporting the Excel data using a built-in feature in Excel. Here’s how to do it step by step:

  1. Open Microsoft Excel, navigate to the desired worksheet containing your data, and click the “File” menu. In the “Export” tab, select the “Change Format” option.
  2. From the list of formats available under the “Save as type:” dropdown, choose “Matlab (*.mat)” and hit the “Save” button to save the Excel worksheet data in MAT-file format.
  3. In Matlab, create a new script file using the built-in editor or a text editor of your choice.
  4. Save the script as a *.m file with any convenient name, e.g., “plotExcelData.m”.
  5. Open the plotExcelData.m script in Matlab’s environment by double-clicking on it or using the built-in function, edit('path/to/file').
  6. In the script file, add the following code to load and read the MAT-file containing your Excel data:
% Import Excel Data into Matlab
fileName = 'path/to/your_data.mat'; % Change path accordingly
[~, matFile] = matlab.io.load(fileName); % Read the MAT-file
data = matFile.yourSheetName; % Assign Excel sheet data to a variable named "data"

Data Manipulation and Analysis in Matlab

Before diving into plotting, it is crucial to clean up the imported data for better visualization. Matlab offers various tools for this purpose, which includes data transformation (e.g., taking logarithms), rescaling, filtering outliers or noise, and more. Here’s a brief overview of these operations:

  1. Data Transformation: Use log, exp, sin or other mathematical functions for transforming numerical values in the dataset. This is helpful when working with data that follows power laws or has exponential relationships.
  2. Rescaling and Normalization: Matlab provides built-in functions like ramp, sqrt, log10, etc., to rescale data points on a logarithmic scale. Data normalization involves adjusting the values in a dataset so they have a mean of zero and a standard deviation of one, making it easier for analysis and comparison purposes.
  3. Filtering Outliers: Matlab offers various functions for outlier detection like zscore, mad (median absolute deviation), etc., to identify abnormal data points that may skew the overall trend in your dataset. It’s essential to remove these outliers before plotting them to ensure accurate representation.

Plotting Excel Data using Matlab Tools

Now, with clean and prepared data in hand, we can utilize Matlab’s vast library of charting tools to visualize the data in various forms such as line plots, scatter plots, pie charts, bar graphs, histograms, boxplots, etc. Some of the most common chart types are:

  1. Line Plots - Useful for displaying changes over time or relationships between variables on a single axis. Matlab has the plot command and its variants like plotyy, subplot, which enable versatile line plotting capabilities.
  2. Scatter Plots - Ideal for depicting multiple data points in two dimensions, highlighting correlations or distribution patterns. Use scatter to generate these visualizations.
  3. Bar Graphs - Great for displaying categorical and grouped data. Matlab provides the bar command for generating bar charts with various customization options like color, orientation, etc.
  4. Pie Charts - Effective for showing proportions of various classes within a dataset. The pie function in Matlab is an excellent tool for creating pie charts.
  5. Histograms - Useful for visualizing frequency distributions of continuous data. Utilize the histc, bar, or plot commands to create histogram plots based on your needs.
  6. Boxplots - Efficient at summarizing and comparing distribution statistics such as median, mean, and interquartile ranges. Matlab’s boxplot function provides an easy way to generate boxplots.

To learn more about these charting tools, explore the online documentation and examples available on MathWorks official website or use built-in help in the Matlab editor with the help command.

Code Samples

In this section, we will provide example code snippets for each of the chart types mentioned earlier. Adjust these snippets to suit your specific data requirements and modify them as per your needs:

  1. Line Plot: Visualize a simple line plot using Matlab’s plot command:
% Generate sample data (x, y) coordinates
t = 0:0.025:1;
y = sin(2*pi*t);

% Plot the data as line chart
figure;
plot(t, y, 'LineWidth', 3, 'Color', 'red');
  1. Scatter Plot: Create a scatter plot using scatter to visualize relationships between two variables:
% Generate sample data (x, y) coordinates
t = 0:0.01:5;
y = sin(2*pi*t);

% Plot the data as a scatter plot
figure;
scatter(t, y);
  1. Bar Graph: Create a bar chart using bar to visualize categorical data:
% Generate sample data for three categories
data = [10 20 30; 40 50 60];
labels = {'Category 1', 'Category 2', 'Category 3'};

% Plot the data as a bar chart
figure;
bar(labels, data);
  1. Pie Chart: Create a pie chart using pie to visualize proportions within a dataset:
% Generate sample data for three categories
data = [10 20 30];
labels = {'Category 1', 'Category 2', 'Category 3'};

% Plot the data as a pie chart
figure;
pie(data, labels);
  1. Histogram: Visualize continuous data using histc to create a histogram:
% Generate sample continuous data
t = randn(1000, 1);

% Create histogram using the given number of bins (6)
figure;
histc(t, 6);
  1. Boxplots: Showcase distribution statistics with boxplot:
% Generate sample data for three categories
data = [10 20 30; 40 50 60];

% Plot the data as a box plot
figure;
boxplot(data);

Conclusion

In summary, with these code samples at your disposal and a thorough understanding of Matlab’s various tools for visualizing Excel data, you will be able to effectively present complex information using graphical representations. This ability is essential in communicating trends, patterns, and insights from the dataset, ultimately improving decision-making processes and promoting better collaboration within your professional setting.