Mastering
MATLAB
A Step-by-Step Guide to Creating Tables and Charts with Numerical Examples
©Soma Mohapatra
Mastering MATLAB
MATLAB is a powerful computational tool used by engineers, scientists, and mathematicians
for data analysis, algorithm development, and modeling. Its capabilities include numerical
calculations, data visualization, and creating interactive applications.
How to Use MATLAB
Getting Started
To begin using MATLAB, you need to install the software on your computer. Once installed,
you can open MATLAB and access its user interface, which includes several key components:
Command Window: The main area where you can enter commands and execute scripts.
Workspace: Displays the variables currently in use.
Current Folder: Shows files and folders in the current directory.
Editor: Allows you to write and edit scripts and functions.
Here is an image of the MATLAB interface to help you get acquainted with its layout:
Basic Operations
You can perform basic arithmetic operations in MATLAB using the Command Window. Here's
a simple exercise to try:
>> a = 5;
>> b = 10;
>> sum = a + b
This code assigns values to a and b, then calculates their sum. The semicolon at the end of a
line suppresses the output, but you can omit it to display results immediately.
Creating Variables
Variables in MATLAB are created when you assign them a value. They can hold different data
types, including numbers, strings, and arrays. For example:
>> x = 3.14; % Creates a scalar variable
>> y = [1, 2, 3]; % Creates a row vector
>> z = [1; 2; 3]; % Creates a column vector
Numerical Example with Tables and Charts
Let's explore a numerical example to demonstrate MATLAB's capabilities with tables and
charts.
Example: Analyzing Sales Data
Suppose you have sales data for a company across four quarters. You want to organize this
data into a table and visualize it using a chart.
Step 1: Organize Data into a Table
First, create arrays for each quarter's sales:
>> Q1 = [2000, 3000, 2500];
>> Q2 = [2200, 3100, 2700];
>> Q3 = [2300, 3200, 2900];
>> Q4 = [2500, 3300, 3100];
Now, organize these arrays into a table:
>> quarters = {'Q1', 'Q2', 'Q3', 'Q4'};
>> productSales = table(quarters', Q1', Q2', Q3', Q4', ...
'VariableNames', {'Quarter', 'ProductA', 'ProductB', 'ProductC'});
The productSales table organizes sales data by quarter and product.
Step 2: Create a Chart
Visualizing data helps identify trends and patterns. Use MATLAB's plotting functions to create
a bar chart:
>> barData = [Q1; Q2; Q3; Q4]';
>> bar(barData);
>> set(gca, 'XTickLabel', {'ProductA', 'ProductB', 'ProductC'});
>> xlabel('Products');
>> ylabel('Sales');
>> title('Quarterly Sales by Product');
>> legend(quarters);
This code generates a bar chart displaying sales data for each product across the four
quarters. Here is a sample of what the bar chart might look like:
Conclusion
By following these steps, you can efficiently use MATLAB for numerical analysis and data
visualization. As you continue to work with MATLAB, explore its extensive documentation and
online resources to expand your skills and tackle more complex projects. Happy coding!
By, Soma Mohapatra
Phone - 9205310305
E-mail - author.soma@gmail.com