0% found this document useful (0 votes)
21 views14 pages

MATLAB Basics & Programming Fundamentals (Autosaved)

Uploaded by

FM Videos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views14 pages

MATLAB Basics & Programming Fundamentals (Autosaved)

Uploaded by

FM Videos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

MATLAB Basics &

Programming
Fundamentals
Course No: 0714 17 CSE 3158
Course Title: MATLAB
What is Matlab?
 Matlab stands for MATrix Laboratory.

It is a programming language and environment designed for:


 Mathematical calculations

 Data analysis and visualization

 Simulations and algorithm development

 Working with matrices and arrays


 Engineering (signal processing,
Where is Matlab used?
control systems, robotics)

 Science (physics, biology, chemistry

simulations)

 Data analysis (machine learning,

statistics)
How Matlab is Different from C
Feature Matlab C
Type Interpreted, high-level Compiled, low-level
Arrays and matrices Basic (arrays, structs,
Data Structures
(very easy to use) pointers)
Very simple for math, More rules, need to
Syntax
more like math notation define types
Slower for low-level Faster for system-level
Speed
tasks tasks
Built-in plotting and
Visualization No built-in plotting
graphing tools
Prototyping, data
Fast, close to hardware,
Usage analysis, math heavy
embedded
tasks
When to Use Matlab vs. C
Use Matlab when: Use C when:
You need to work with You need fast, efficient
math, matrices, or data code for hardware or
analysis embedded systems
You need to control
You want to plot data or
memory and optimize
visualize results
speed
Rapid prototyping and You’re writing drivers,
testing operating systems, etc.
Basic Matlab Commands
Arithmetic Operations +, -, *, /, ^
Clearing Variables clear
Clearing Command
clc
Window
Use % for single-line
Commenting and comments
Documentation Use %% to create sections
in scripts
Matlab Basics: Syntax and
Structure
Let’s look at some side-by-side comparisons.
C example: Matlab example:

#include <stdio.h> disp('Hello, world!')


int main() {
printf("Hello, world!\
n");
return 0;
}
Note: Matlab does not need a main function or special libraries for simple things.
Variables and Data Types

C example: Matlab example:

int x = 5; x = 5; % No need to declare type


float y = 2.5; y = 2.5;
char name[] = "Matlab"; name = ‘Matlab’; % Use single quotes for
strings

Note: . In Matlab, you don’t declare variable types. It figures it out automatically.
Arrays and Matrices
C example:

int a[3] = {1, 2, 3};


float matrix[2][2] = { {1.1, 1.2}, {2.1, 2.2} };

Matlab example:

a = [1 2 3]; % Row vector


matrix = [1.1 1.2; 2.1 2.2]; % 2x2 matrix

Note: Matlab was made for matrices! You just type numbers in square
brackets. Use spaces for columns and semicolons for rows..
Loops

C example: Matlab example:

for (int i = 0; i < 5; i++) for i = 0:4


{
disp(i)
printf("%d\n", i);
end
}

Note: . In Matlab, 0:4 means 0 to 4. You don’t need


parentheses or curly braces.
If-Else Statements
C example: Matlab example:

int x = 10; x = 10;


if (x > 5) { if x > 5
printf("x is greater than 5\n"); disp('x is greater than 5')

} else { else
disp('x is not greater than 5')
printf("x is not greater than 5\
n"); end
}
Note: . In Matlab, use end to finish the block in
Matlab.
Example: Addition of Two Numbers

C example: Matlab example:

int a = 3, b = 7; a = 3; b = 7;

int sum = a + b; sum = a + b;


disp(['Sum is ',
printf("Sum is %d\n", sum); num2str(sum)])

Note: In Matlab, num2str turns a number into a


string for display.
Matlab Functions
What is a function?
Reusable code block with inputs/outputs

function y = squareNumber(x)
y = x^2;
end

Calling Function:
result = squareNumber(4);
Plotting a Graph
Matlab example:
C example:

No built-in support; x = 0:0.1:2*pi; % 0 to 2*pi in steps of 0.1


y = sin(x);
need extra libraries.
plot(x, y)
title('Sine Wave')
xlabel('x')
ylabel('sin(x)')

Note: Matlab can plot with


just a few lines!

You might also like