Handling Data in MATLAB
Handling data is one of MATLAB’s most powerful features, enabling users to efficiently manipulate, analyze, and visualize data in various formats. This section provides an in-depth guide to managing data in MATLAB, including reading, writing, and processing data.
Importing Data
MATLAB supports multiple formats for importing data, such as text files, Excel spreadsheets, and MAT-files. Here are some examples:
% Reading data from a text file
data = readtable('data.txt');
disp(data);
% Reading data from an Excel file
data = readmatrix('data.xlsx');
disp(data);
% Loading MAT-file
load('data.mat'); % Loads variables saved in 'data.mat'
disp(myVariable); % Displays the loaded variable
Example output:
data.txt content: Name Age Score Alice 23 88 Bob 25 92 MATLAB output (readtable): Name Age Score ____ ____ _____ 'Alice' 23 88 'Bob' 25 92
Exporting Data
Exporting data is equally important. MATLAB provides several commands for saving data to files:
% Writing data to a text file
writetable(data, 'output.txt');
% Writing data to an Excel file
writematrix(data, 'output.xlsx');
% Saving data to a MAT-file
save('output.mat', 'myVariable'); % Saves the variable to 'output.mat'
Check your current directory to view the created files.
Data Preprocessing
Data often needs preprocessing before analysis. Here are some common preprocessing tasks:
Handling Missing Data
% Handling missing data
data = [1 NaN 3; 4 5 NaN; 7 8 9];
disp('Original Data:');
disp(data);
% Replace missing data with zeros
data(isnan(data)) = 0;
disp('After Replacing NaN with 0:');
disp(data);
Example output:
Original Data: 1 NaN 3 4 5 NaN 7 8 9 After Replacing NaN with 0: 1 0 3 4 5 0 7 8 9
Filtering Data
% Filtering data using logical indexing
data = [1 5 3; 4 2 6; 7 8 9];
disp('Original Data:');
disp(data);
% Extract rows where the first column is greater than 3
filteredData = data(data(:,1) > 3, :);
disp('Filtered Data:');
disp(filteredData);
Example output:
Original Data: 1 5 3 4 2 6 7 8 9 Filtered Data: 4 2 6 7 8 9
Advanced Filtering: Conditional Selection
% Advanced filtering based on multiple conditions
data = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
disp('Original Data:');
disp(data);
% Select rows where the first column is greater than 5
% AND the third column is less than 12
filteredData = data(data(:,1) > 5 & data(:,3) < 12, :);
disp('Filtered Data:');
disp(filteredData);
Example output:
Original Data: 1 2 3 4 5 6 7 8 9 10 11 12 Filtered Data: 7 8 9
Data Transformation
Transforming data is essential for reshaping it for further analysis. Examples include reshaping, sorting, and scaling data:
Reshaping Data
% Reshaping data
data = [1 2 3 4 5 6 7 8 9];
reshapedData = reshape(data, 3, 3);
disp('Reshaped Data:');
disp(reshapedData);
Example output:
Reshaped Data: 1 4 7 2 5 8 3 6 9
Sorting Data
% Sorting data
data = [3 1 2; 6 4 5; 9 7 8];
sortedData = sort(data, 2); % Sort rows in ascending order
disp('Sorted Data:');
disp(sortedData);
Example output:
Sorted Data: 1 2 3 4 5 6 7 8 9
Rescaling Data to a Specific Range
% Rescaling data to the range [0, 1]
data = [10, 20, 30, 40, 50];
disp('Original Data:');
disp(data);
% Normalize the data to range [0, 1]
minVal = min(data);
maxVal = max(data);
scaledData = (data - minVal) / (maxVal - minVal);
disp('Rescaled Data (0 to 1):');
disp(scaledData);
Example output:
Original Data: 10 20 30 40 50 Rescaled Data (0 to 1): 0 0.25 0.50 0.75 1.00
Z-Score Normalization
% Applying Z-score normalization
data = [100, 200, 300, 400, 500];
disp('Original Data:');
disp(data);
% Compute Z-score normalization
meanVal = mean(data);
stdVal = std(data);
zScoreData = (data - meanVal) / stdVal;
disp('Z-Score Normalized Data:');
disp(zScoreData);
Example output:
Original Data: 100 200 300 400 500 Z-Score Normalized Data: -1.2649 -0.6325 0 0.6325 1.2649
Logarithmic Transformation
% Applying a logarithmic transformation
data = [1, 10, 100, 1000, 10000];
disp('Original Data:');
disp(data);
% Apply base-10 logarithmic transformation
logTransformedData = log10(data);
disp('Logarithmically Transformed Data (base-10):');
disp(logTransformedData);
Example output:
Original Data: 1 10 100 1000 10000 Logarithmically Transformed Data (base-10): 0 1 2 3 4
Combining and Reordering Columns
% Combining and reordering columns
data1 = [1, 2, 3; 4, 5, 6; 7, 8, 9];
data2 = [10, 20, 30; 40, 50, 60; 70, 80, 90];
disp('Original Data 1:');
disp(data1);
disp('Original Data 2:');
disp(data2);
% Combine the two datasets by concatenating columns
combinedData = [data1, data2];
disp('Combined Data:');
disp(combinedData);
% Reorder columns (switch the first and last columns)
reorderedData = combinedData(:, [4, 2, 3, 1, 5, 6]);
disp('Reordered Data:');
disp(reorderedData);
Example output:
Original Data 1: 1 2 3 4 5 6 7 8 9 Original Data 2: 10 20 30 40 50 60 70 80 90 Combined Data: 1 2 3 10 20 30 4 5 6 40 50 60 7 8 9 70 80 90 Reordered Data: 10 2 3 1 20 30 40 5 6 4 50 60 70 8 9 7 80 90
Categorizing Data with Custom Labels
% Categorizing data into custom labels
data = [5, 15, 25, 35, 45];
disp('Original Data:');
disp(data);
% Define categories based on ranges
categories = cell(size(data));
for i = 1:length(data)
if data(i) < 10
categories{i} = 'Low';
elseif data(i) <= 30
categories{i} = 'Medium';
else
categories{i} = 'High';
end
end
disp('Categories:');
disp(categories);
Example output:
Original Data: 5 15 25 35 45 Categories: 'Low' 'Medium' 'Medium' 'High' 'High'
Useful MATLAB Functions for Handling Data
Practice Questions
Test Yourself
1. Import data from a text file, replace all missing values with -1, and save it to a new file.
2. Create a 3×3 matrix of random numbers and sort its rows in descending order.
3. Extract all rows from a matrix where the second column is greater than 5.