MATLAB is an excellent tool for engineers and scientists to import complex data sets from a wide variety of sources (Hadoop, Spark, etc.) and fields ( climatology, predictive maintenance, medical research, and finance) and analyze it through the prebuilt functions that have advanced analytic techniques to help them process big data.
The visualizations created by these analyses are customizable and interactive and can be shared over a wide variety of platforms. This article will guide you how to use some basic data analysis tools in MATLAB for different data types so you can start exploring this useful software at ease. If you are wondering ‘what is a machine learning model’ you will find out more in the regression analysis section.
Importing data
The first step when learning how to use MATLAB is to learn how to import data into the software from whichever source of data you are working with. This can be done using the Import Tool.
On the Home tab, in the Variable section, click Import Data. The Import Tool imports data from spreadsheet and text files, for other types of files, the Import Wizard tool is used.
uiimport(filename) opens the file specified in filename.
Alternatively, load(‘filename’) opens any file that has been given a MATLABPATH or full file pathname.
Signal smoothing
As the software is primarily made for engineers and scientists, data is usually in the form of signals collected over time from different kinds of devices. The fluctuations in this kind of data makes it very difficult to see the pattern so let’s learn how to reduce the noise and see a clear pattern in the form of a graph plotted against time. For instance, let’s import the temperature signals collected over time at Boston Airport and see how it looks.
load bostemp
days = (1:31*24)/24;
plot(days, tempC)
axis tight
ylabel(‘Temp (\circC)’)
xlabel(‘Time elapsed from Jan 1, 2011 (days)’)
title(‘Logan Airport Dry Bulb Temperature (source: NOAA)’)
This loads the data as follows

It is difficult to observe a pattern here because it shows the temperature every hour. In order to remove the hourly variations and only see the daily temperature over a month, we use the Moving Average Filter function which has the following syntax
hoursPerDay = 24;
coeff24hMA = ones(1, hoursPerDay)/hoursPerDay;
avg24hTempC = filter(coeff24hMA, 1, tempC);
plot(days,[tempC avg24hTempC])
legend(‘Hourly Temp’,’24 Hour Average (delayed)’,’location’,’best’)
ylabel(‘Temp (\circC)’)
xlabel(‘Time elapsed from Jan 1, 2011 (days)’)
title(‘Logan Airport Dry Bulb Temperature (source: NOAA)’)
This generates the following graph (red line). So now your data is smoother and clearer.

Color-based segmentation
Did you know that you can separate image pixels by value and distribute them in different colour clusters. This kind of image analysis is highly useful in pathology. The following image shows a tissue stained with hematoxylin and eosin but the pathologist wants to observe the clusters in blue, purple and pink separately. The first step is to import the image
he = imread(“hestain.png”);
imshow(he)
title(“H&E Image”)
text(size(he,2),size(he,1)+15, …
“Image courtesy of Alan Partin, Johns Hopkins University”, …
FontSize=7,HorizontalAlignment=”right”)

After this, convert the image to RGB color space and then to L*a*b* Color Space using the following code
numColors = 3;
L = imsegkmeans(he,numColors);
B = labeloverlay(he,L);
imshow(B)
title(“Labeled Image RGB”)
lab_he = rgb2lab(he);
Then the following code is applied to classify colors into a*b* space
ab = lab_he(:,:,2:3);
ab = im2single(ab);
pixel_labels = imsegkmeans(ab,numColors,NumAttempts=3);
To create the following image, this code is finally applied
B2 = labeloverlay(he,pixel_labels);
imshow(B2)
title(“Labeled Image a*b*”)

To view one of the clusters, like in the image below, the following code is applied to the above image. Similarly, code can be applied for other colour clusters.
mask1 = pixel_labels == 1;
cluster1 = he.*uint8(mask1);
imshow(cluster1)
title(“Objects in Cluster 1”);

Regression analysis
A data model is required to see the relationship between predictor and response variables and this is where the importance of regression analysis lies. For simple linear regression analysis of two variables that have been established as linearly related, the following code is followed
Step 1: Let’s work with a file that contains data about accidents (y) that happened in different US states population (x). This data is in the form of a matrix and this regression model is applied on it.

The following code is entered in the working space:
load accidents
x = hwydata(:,14); %Population of states
y = hwydata(:,4); %Accidents per state
format long
b1 = x\y
b1 = 1.372716735564871e-04
The linear relation is found to be The linear relation is y=β1x=0.0001372x.
This is then plotted as a graph using the following code
yCalc1 = b1*x;
scatter(x,y)
hold on
plot(x,yCalc1)
xlabel(‘Population of state’)
ylabel(‘Fatal traffic accidents per state’)
title(‘Linear Regression Relation Between Accidents & Population’)
grid on

If this seems related to your work and you now don’t feel scared by the question ‘what is MATLAB’, you can install MATLAB here for your OS- the MATLAB download and installation process is clearly explained. There are hundreds of functions that can be simpler or more complex than the ones mentioned above. You can browse through those here and find the one that fits your data analysis needs. The simplest function that you might need is the plot function that tells you how to plot in MATLAB. If you want to explore more functions like the regression model, you can peruse through the different machine learning models.





















