MATLAB Programming Workbook
MATLAB Programming Workbook
Table of Contents
Module I: Introduction
Module V: M-Files
Learning Objectives:
Topics Covered:
radius = 5;
area = pi * radius^2;
a = 10;
b = 20;
if a > b
else
end
Exercises:
Learning Objectives:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A * B;
disp(C);
A = [1, 2, 3];
B = [4, 5, 6];
C = A .* B; % Element-wise multiplication
disp('Element-wise multiplication:');
disp(C);
disp(cellArray{1});
student.name = 'John';
student.age = 21;
student.marks = [85, 90, 78];
disp('Student structure:');
disp(student);
Exercises:
Learning Objectives:
x = 0:0.1:10;
y = sin(x);
title('Sine Wave');
xlabel('x');
ylabel('sin(x)');
grid on;
legend('sin(x)', 'cos(x)');
title('Trigonometric Functions');
sound(y, Fs);
img = imread('example.jpg');
imshow(img);
title('Loaded Image');
Examples: Create a bar plot from data.
bar(data);
title('Bar Plot');
xlabel('Categories');
ylabel('Values');
Explanation: 'bar' creates a bar chart, and we set labels and title for better
visualization.
img = imread('example.jpg');
grayImg = rgb2gray(img);
imshow(grayImg);
title('Grayscale Image');
plot(y);
title('Sound Waveform');
xlabel('Time');
ylabel('Amplitude');
Explanation: 'audioread' reads the sound file, and 'plot' visualizes the
waveform.
Exercises:
Learning Objectives:
x = 10;
if mod(x, 2) == 0
disp('x is even');
else
disp('x is odd');
end
for i = 1:5
end
for i = 1:10
disp(i);
end
Explanation: 'for' loop iterates over numbers from 1 to 10, and 'disp' prints
each number.
factorial = 1;
for i = 1:number
factorial = factorial * i;
end
switch dayNum
case 1
disp('Monday');
case 2
disp('Tuesday');
case 3
disp('Wednesday');
case 4
disp('Thursday');
case 5
disp('Friday');
case 6
disp('Saturday');
case 7
disp('Sunday');
otherwise
end
Exercises:
1. Write a program to display numbers from 1 to 10 using a loop.
Learning Objectives:
result = x^2;
end
data = rand(10);
disp('Loaded Data:');
disp(data);
Exercises:
Description:
Learn how to visualize data, process sound files, and handle image data.
Examples:
matlab
Copy code
bar(data);
title('Bar Plot');
xlabel('Categories');
ylabel('Values');
% Explanation: 'bar' creates a bar chart, and we set labels and title for
better visualization.
matlab
Copy code
img = imread('example.jpg');
grayImg = rgb2gray(img);
imshow(grayImg);
title('Grayscale Image');
matlab
Copy code
% Program to read and visualize a sound file
plot(y);
title('Sound Waveform');
xlabel('Time');
ylabel('Amplitude');
% Explanation: 'audioread' reads the sound file, and 'plot' visualizes the
waveform.
Description:
Understand conditional statements, loops, and user-defined functions.
Examples:
matlab
Copy code
for i = 1:10
disp(i);
end
% Explanation: 'for' loop iterates over numbers from 1 to 10, and 'disp'
prints each number.
matlab
Copy code
factorial = 1;
for i = 1:number
factorial = factorial * i;
end
matlab
Copy code
switch dayNum
case 1
disp('Monday');
case 2
disp('Tuesday');
case 3
disp('Wednesday');
case 4
disp('Thursday');
case 5
disp('Friday');
case 6
disp('Saturday');
case 7
disp('Sunday');
otherwise
end
% Explanation: 'switch' matches the case based on 'dayNum' and displays
the corresponding day.