Learn MATLAB - 10 Code Examples & CST Typing Practice Test
MATLAB (Matrix Laboratory) is a high-level, interpreted programming language and computing environment developed by MathWorks. It is optimized for numerical computation, data analysis, visualization, algorithm development, and simulation, making it widely used in engineering, scientific research, and applied mathematics.
Learn MATLAB with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
MATLAB Counter and Theme Toggle
count = 0;
isDark = false;
function updateUI()
disp(['Counter: ', num2str(count)]);
disp(['Theme: ', ternary(isDark, 'Dark', 'Light')]);
end
function increment()
global count;
count = count + 1;
updateUI();
end
function decrement()
global count;
count = count - 1;
updateUI();
end
function reset()
global count;
count = 0;
updateUI();
end
function toggleTheme()
global isDark;
isDark = ~isDark;
updateUI();
end
% Simulate actions
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
Demonstrates a simple counter with theme toggling using MATLAB scripts and console output.
MATLAB Random Number Generator
for i = 1:3
num = randi([1, 100]);
disp(['Random ', num2str(i), ': ', num2str(num)]);
end
Generates random numbers between 1 and 100 and displays them.
MATLAB Todo List
todos = {};
function addTask(task)
global todos;
todos{end+1} = task;
disp(todos);
end
function removeTask(index)
global todos;
todos(index) = [];
disp(todos);
end
% Simulate actions
addTask('Buy milk');
addTask('Write MATLAB code');
removeTask(1);
Adds and removes tasks from a todo list.
MATLAB Dice Roller
for i = 1:3
roll = randi([1,6]);
disp(['Roll ', num2str(i), ': ', num2str(roll)]);
end
Rolls a six-sided dice three times.
MATLAB Countdown Timer
count = 5;
while count >= 0
disp(['Countdown: ', num2str(count)]);
count = count - 1;
end
disp('Done!');
Counts down from 5 to 0.
MATLAB Prime Checker
nums = [7, 10, 13];
for i = 1:length(nums)
n = nums(i);
if isprime(n)
disp([num2str(n), ' is Prime']);
else
disp([num2str(n), ' is Not Prime']);
end
end
Checks if numbers are prime.
MATLAB Temperature Converter
cToF = @(c) c*9/5 + 32;
fToC = @(f) (f-32)*5/9;
disp(['25°C = ', num2str(cToF(25)), '°F']);
disp(['77°F = ', num2str(fToC(77)), '°C']);
Converts Celsius to Fahrenheit and Fahrenheit to Celsius.
MATLAB Shopping Cart
cart = {};
prices = [];
def addItem(item, price)
global cart prices;
cart{end+1} = item;
prices(end+1) = price;
disp(cart);
disp(['Total: ', num2str(sum(prices))]);
end
def removeItem(index)
global cart prices;
cart(index) = [];
prices(index) = [];
disp(cart);
disp(['Total: ', num2str(sum(prices))]);
end
% Simulate actions
addItem('Apple', 2);
addItem('Banana', 3);
removeItem(1);
Adds and removes items in a shopping cart with total cost.
MATLAB Name Greeting
function greet(name)
disp(['Hello, ', name, '! Welcome!']);
end
% Simulate actions
greet('Saurav');
greet('Alice');
greet('Bob');
Greets users by name.
MATLAB Stopwatch
time = 0;
while time < 5
disp(['Stopwatch: ', num2str(time), ' seconds']);
time = time + 1;
end
disp('Done!');
Simulates a stopwatch incrementing seconds.
Frequently Asked Questions about MATLAB
What is MATLAB?
MATLAB (Matrix Laboratory) is a high-level, interpreted programming language and computing environment developed by MathWorks. It is optimized for numerical computation, data analysis, visualization, algorithm development, and simulation, making it widely used in engineering, scientific research, and applied mathematics.
What are the primary use cases for MATLAB?
Numerical computation and algorithm development. Data analysis and visualization. Signal, image, and video processing. Control systems and simulations. Machine learning and AI prototyping
What are the strengths of MATLAB?
Rapid prototyping and algorithm development. Extensive documentation and support. Highly optimized for matrix computations. Strong visualization and plotting tools. Wide adoption in industry and academia
What are the limitations of MATLAB?
Proprietary software with licensing costs. Slower than compiled languages for some applications. Not ideal for low-level system programming. Large memory footprint for huge datasets. Limited general-purpose programming features
How can I practice MATLAB typing speed?
CodeSpeedTest offers 10+ real MATLAB code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.