MATLAB Exam Notes - Code Flow Diagrams and Color Models
MATLAB Exam Notes
Question 1: MATLAB Code Flow Diagrams
Question 2: Color Model / Color Coding in MATLAB
How to use this PDF
Revise the definitions first, then memorize the symbols and 4 common examples: add two numbers,
even/odd, loop sum, and RGB color model. In the exam, write definition + diagram + code + short
explanation.
Topic What to remember
MATLAB code flow diagram A visual representation of how MATLAB code runs step by step: start, input,
process, decision, loop, output, and end.
Color model A numerical method to represent colors in digital images. Common models:
RGB, grayscale, HSV, and CMYK.
Color coding in MATLAB MATLAB can use color names, short color letters, RGB triplets, and image color
channels.
Page 1
MATLAB Exam Notes - Code Flow Diagrams and Color Models
Question 1 - MATLAB Code Flow Diagram
A MATLAB code flow diagram is a flowchart that shows the logical order in which a MATLAB program
executes. It explains how the program starts, takes input, performs calculations, checks decisions,
repeats loops, displays output, and ends.
Exam definition
A MATLAB code flow diagram is a graphical representation of the logical sequence of a MATLAB program.
It helps in planning, understanding, debugging, and explaining code before writing or running it in
MATLAB.
1.1 General flow of a MATLAB program
Start
|
Input data
|
Initialize variables
|
Process / calculation
|
Decision or loop if required
|
Display output
|
End
Flowchart symbol Meaning MATLAB example
Oval Start or End of program Start, End
Parallelogram Input or Output input(), disp(), fprintf()
Rectangle Process or calculation sum = a + b
Diamond Decision / condition if x > 0
Arrow Direction of execution Shows next step
Connector Connects long diagrams Used when diagram continues
1.2 Important MATLAB control flow commands
Command Purpose Simple meaning
input() Takes data from user Enter value
disp() Displays output Show result
if / else Makes a decision True or false path
elseif Checks multiple conditions More than two cases
for Repeats fixed number of times Known repetitions
while Repeats while condition is true Condition-based repetition
switch Selects one case from many Menu or choice based logic
break Stops loop immediately Exit loop
continue Skips one iteration Move to next repetition
end Closes if, loop, switch, or function Block finished
Page 2
MATLAB Exam Notes - Code Flow Diagrams and Color Models
1.3 Easy example - Add two numbers
This is a simple sequential flow because every line runs one after another from top to bottom.
MATLAB code
a = input('Enter first number: ');
b = input('Enter second number: ');
sum = a + b;
disp(sum)
Flow diagram
Start
|
Input a, b
|
sum = a + b
|
Display sum
|
End
1.4 Easy decision example - Even or odd number
This example uses a decision block because the answer can be even or odd.
MATLAB code
n = input('Enter a number: ');
if mod(n,2) == 0
disp('Even number')
else
disp('Odd number')
end
Flow diagram
Start
|
Input n
|
Is mod(n,2) == 0?
|Yes |No
v v
Display Even Display Odd
| |
+---------> End <----+
Key point
mod(n,2) gives the remainder when n is divided by 2. If the remainder is 0, the number is even.
Page 3
MATLAB Exam Notes - Code Flow Diagrams and Color Models
1.5 Medium example - Largest of three numbers
This example uses if, elseif, else, and the logical operator && which means AND.
MATLAB code
a = input('Enter first number: ');
b = input('Enter second number: ');
c = input('Enter third number: ');
if a > b && a > c
disp('a is greatest')
elseif b > a && b > c
disp('b is greatest')
else
disp('c is greatest')
end
Flow diagram
Start
|
Input a, b, c
|
Is a > b AND a > c?
|Yes |No
v v
Display a greatest Is b > a AND b > c?
|Yes |No
v v
Display b greatest Display c greatest
| |
+------> End <-------+
1.6 Medium loop example - Sum of first n numbers
This example uses a for loop. Always initialize the sum before the loop.
MATLAB code
n = input('Enter value of n: ');
sum = 0;
for i = 1:n
sum = sum + i;
end
disp(sum)
Flow diagram
Start
|
Input n
|
sum = 0, i = 1
|
Is i <= n?
|Yes |No
v v
sum = sum + i Display sum
| |
i = i + 1 End
|
Go back to condition
Page 4
MATLAB Exam Notes - Code Flow Diagrams and Color Models
1.7 Complicated example - Factorial of a number
Factorial means multiplying a number by all positive integers less than it. Example: 5! = 5 x 4 x 3 x 2 x 1 =
120.
MATLAB code
n = input('Enter a number: ');
fact = 1;
for i = 1:n
fact = fact * i;
end
disp(fact)
Flow diagram
Start
|
Input n
|
fact = 1, i = 1
|
Is i <= n?
|Yes |No
v v
fact = fact * i Display fact
| |
i = i + 1 End
|
Go back to condition
1.8 Complicated example - Prime number checking
A prime number is divisible only by 1 and itself. This example checks whether n is divisible by any
number from 2 to n-1.
MATLAB code
n = input('Enter a number: ');
flag = 0;
for i = 2:n-1
if mod(n,i) == 0
flag = 1;
break
end
end
if flag == 0
disp('Prime number')
else
disp('Not prime number')
end
Simplified flow diagram
Page 5
MATLAB Exam Notes - Code Flow Diagrams and Color Models
Start
|
Input n
|
flag = 0, i = 2
|
Is i <= n-1?
|Yes |No
v v
Is mod(n,i) == 0? Is flag == 0?
|Yes |No |Yes |No
v v v v
flag = 1 i = i + 1 Display Prime Display Not Prime
break |
| |
+-----> Final checking
Page 6
MATLAB Exam Notes - Code Flow Diagrams and Color Models
1.9 Example - Plotting a graph in MATLAB
MATLAB is commonly used to plot graphs. In this example, x values are created, y values are calculated,
and then a sine graph is plotted.
MATLAB code
x = 0:0.1:10;
y = sin(x);
plot(x,y)
xlabel('x-axis')
ylabel('y-axis')
title('Sine Wave')
Flow diagram
Start
|
Create x values from 0 to 10
|
Calculate y = sin(x)
|
Plot x and y
|
Add labels and title
|
End
1.10 Short exam answer
Write this in the exam
A MATLAB code flow diagram is a graphical representation of the step-by-step logic of a MATLAB
program. It shows input, processing, decisions, loops, output, and end of the program. It helps
programmers understand and debug the program before writing the final MATLAB code.
Common mistakes to avoid
- Do not forget Start and End.
- Use a diamond shape for decisions such as if conditions.
- Use arrows to show the direction of execution.
- Initialize variables before loops, for example sum = 0 or fact = 1.
- Close if, for, while, switch, and function blocks with end.
- In a while loop, update the loop variable, otherwise an infinite loop may occur.
Page 7
MATLAB Exam Notes - Code Flow Diagrams and Color Models
Question 2 - Color Model and Color Coding in MATLAB
A color model is a system used to represent colors using numbers. Computers do not understand colors
like humans do; they store colors as numerical values.
Exam definition
A color model is a mathematical system used to represent colors in digital images. It defines how colors
are created, stored, processed, and displayed using numerical values. Common color models include
RGB, grayscale, HSV, and CMYK.
2.1 Why color models are used
- To display digital images on screens.
- To store color information in images.
- To process images in MATLAB.
- To detect colors, objects, and regions in images.
- To print images using printer color systems.
- To control graph colors and visual appearance.
2.2 Common color models
Color model Full form / idea Main use
RGB Red, Green, Blue Screens, MATLAB images, cameras, monitors
Grayscale Intensity from black to white Black-and-white images and image processing
HSV Hue, Saturation, Value Color detection and human-like color description
CMYK Cyan, Magenta, Yellow, Black Printing
2.3 RGB color model
RGB is the most common color model for screens and MATLAB images. Every color is made by mixing
Red, Green, and Blue.
Color RGB value in 0-255 form Meaning
Red [255, 0, 0] Maximum red, no green, no blue
Green [0, 255, 0] Maximum green only
Blue [0, 0, 255] Maximum blue only
White [255, 255, 255] All channels maximum
Black [0, 0, 0] All channels zero
Yellow [255, 255, 0] Red plus green
Cyan [0, 255, 255] Green plus blue
Magenta [255, 0, 255] Red plus blue
MATLAB image point
A color RGB image in MATLAB is usually stored as a 3D matrix of size rows x columns x 3. The three
channels are red, green, and blue.
Page 8
MATLAB Exam Notes - Code Flow Diagrams and Color Models
2.4 RGB image channels in MATLAB
In MATLAB, an RGB image has three channels. The first channel stores red values, the second stores
green values, and the third stores blue values.
img = imread('[Link]');
redChannel = img(:,:,1);
greenChannel = img(:,:,2);
blueChannel = img(:,:,3);
imshow(img)
RGB image size example:
100 x 100 x 3
100 rows
100 columns
3 color channels: Red, Green, Blue
2.5 Grayscale color model
A grayscale image contains only shades of gray. It has one intensity channel instead of three color
channels.
Value Color meaning
0 Black
50 Dark gray
128 Medium gray
255 White
MATLAB code
img = imread('[Link]');
gray = rgb2gray(img);
imshow(gray)
2.6 HSV color model
HSV describes color in a way closer to human vision. It separates the color itself from its intensity and
brightness.
HSV part Meaning Simple explanation
Hue Actual color Red, yellow, green, blue, etc.
Saturation Purity or strength of color Low saturation looks grayish; high saturation looks
strong.
Value Brightness Low value is dark; high value is bright.
MATLAB code
img = imread('[Link]');
hsv_img = rgb2hsv(img);
imshow(hsv_img)
Where HSV is useful
HSV is useful when you want to detect or separate objects based on color, such as detecting a red ball,
green leaf, or blue object in an image.
Page 9
MATLAB Exam Notes - Code Flow Diagrams and Color Models
2.7 CMYK color model
CMYK stands for Cyan, Magenta, Yellow, and Black. It is mostly used for printing because printers use
ink.
Model Used for Basic idea
RGB Screens Adds light: red, green, blue
CMYK Printers Uses ink: cyan, magenta, yellow, black
2.8 Color coding in MATLAB plots
Color coding means using different colors, line styles, and markers to make graphs easier to
understand. MATLAB allows short color codes and RGB triplets.
Color code Color
'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
Example: red dashed line with circle markers
x = 1:5;
y = [2 4 3 5 7];
plot(x, y, 'r--o')
xlabel('x')
ylabel('y')
title('Red Dashed Line with Circle Markers')
Part Meaning
r Red color
-- Dashed line
o Circle marker
2.9 Line styles and markers
Code Meaning Example use
'-' Solid line plot(x,y,'b-')
'--' Dashed line plot(x,y,'r--')
':' Dotted line plot(x,y,'k:')
'-.' Dash-dot line plot(x,y,'g-.')
'o' Circle marker plot(x,y,'bo')
'*' Star marker plot(x,y,'r*')
's' Square marker plot(x,y,'ks')
Page 10
MATLAB Exam Notes - Code Flow Diagrams and Color Models
Code Meaning Example use
'^' Triangle marker plot(x,y,'g^')
Page 11
MATLAB Exam Notes - Code Flow Diagrams and Color Models
2.10 RGB triplets for custom plot colors
In MATLAB plots, you can also define color by RGB triplets. For plotting, RGB values usually range from 0
to 1, not 0 to 255.
Color RGB triplet for MATLAB plot
Red [1 0 0]
Green [0 1 0]
Blue [0 0 1]
Black [0 0 0]
White [1 1 1]
Orange-like [1 0.5 0]
MATLAB code
x = 0:0.1:10;
y = sin(x);
plot(x, y, 'Color', [0 0.5 1], 'LineWidth', 2)
title('Custom RGB Color')
2.11 Important MATLAB image commands
Command Purpose
imread() Reads an image file into MATLAB
imshow() Displays an image
rgb2gray() Converts RGB image to grayscale
rgb2hsv() Converts RGB image to HSV
hsv2rgb() Converts HSV image to RGB
size() Shows matrix dimensions of an image
plot() Plots data using colors, line styles, and markers
2.12 RGB vs grayscale
RGB image Grayscale image
Has colors Has only black, white, and gray shades
Has 3 channels Has 1 channel
Stored as rows x columns x 3 Stored as rows x columns
Red, green, blue information Only intensity information
Used for colored images Used for simpler image processing
2.13 Short exam answer
Write this in the exam
A color model is a numerical system for representing colors in digital images. RGB represents colors
using red, green, and blue channels; grayscale represents intensity from black to white; HSV represents
hue, saturation, and value; and CMYK is used mainly for printing. In MATLAB, color images are commonly
stored as RGB matrices with three channels.
Page 12
MATLAB Exam Notes - Code Flow Diagrams and Color Models
Final one-page revision
Concept Must-remember points
MATLAB A high-level programming language and software environment for numerical
computing, graphs, simulation, and image processing.
Code flow diagram Visual logic of code: Start -> Input -> Process -> Decision/Loop -> Output -> End.
Flowchart symbols Oval = Start/End, parallelogram = Input/Output, rectangle = Process, diamond =
Decision.
disp Built-in MATLAB function used to display output in the Command Window.
for loop Used when number of repetitions is known.
while loop Used when repetition depends on a condition.
Color model Numerical way to represent colors.
RGB Red, green, blue; used for screens and MATLAB images.
Grayscale One channel, intensity from black to white.
HSV Hue, saturation, value; useful for color detection.
MATLAB plot colors Use 'r', 'g', 'b', 'k' or RGB triplets like [1 0 0].
Very short memorization lines:
1. Flow diagram shows the step-by-step execution logic of MATLAB code.
2. MATLAB code normally runs from top to bottom.
3. A decision in a flowchart is shown by a diamond.
4. RGB image in MATLAB has 3 channels: Red, Green, and Blue.
5. Grayscale image has 1 channel: intensity.
6. Color coding in MATLAB graphs makes different data lines easy to identify.
Page 13