0% found this document useful (0 votes)
6 views12 pages

Understanding MATLAB: Uses & Syntax

Matlab basic definition

Uploaded by

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

Understanding MATLAB: Uses & Syntax

Matlab basic definition

Uploaded by

mujahid.ckd4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1

What is MATLAB?
MATLAB stands for Matrix Laboratory. It is a high-level programming language and interactive computing
environment mainly used for:

 Mathematics and numerical computation

 Data analysis and data processing

 Graph and plot generation

 Engineering and scientific research

 Modeling, simulation, and algorithm development

MATLAB is widely used because of its powerful built-in functions and matrix-based approach, which make it ideal
for technical computing.
It is developed by MathWorks.

⭐ Uses of MATLAB
MATLAB is used in a wide range of technical and scientific fields. The major uses include:

1. Numerical Computation

 Solving equations, matrices, calculus problems, and mathematical models.

2. Data Analysis & Processing

 Importing, cleaning, manipulating, and analyzing large datasets.

3. Visualization

 Creating 2D and 3D plots, graphs, charts, and animations.

4. Matrix Operations

 Performing matrix manipulations such as inverse, transpose, eigenvalues, etc.

5. Algorithm Development

 Designing, testing, and implementing mathematical and engineering algorithms.

6. Simulation & Modeling

 Simulating dynamic systems and building models using Simulink.

7. Engineering Applications

 Signal processing, control systems, image processing, robotics, communication systems.

8. Scientific Research

 Creating research models, running experiments, and analyzing scientific data.

9. Application Development
2
 Building custom user interfaces (GUI) and standalone applications.

10. Integration with Other Languages

 Interfacing MATLAB code with C, C++, Java, Python, and other languages.

 Definition of MATLAB Interface


 The MATLAB interface, also called the MATLAB window, is the main working
environment where users write commands, create programs, view variables, and run
scripts. It consists of several important parts that help in performing different tasks
smoothly:
 1. Toolstrip
 Located at the top; contains tabs such as Home, Plots, Apps, Editor, and provides
buttons for creating files, running code, importing data, and changing the layout.
 2. Current Folder
 Shows the folder where MATLAB is currently working. All files, scripts, and functions
inside this folder can be directly accessed and executed.
 3. Workspace
 Displays all variables that are currently stored in memory. Their names, values, and sizes
can be seen here.
 4. Command Window
 The most important area where users type MATLAB commands and immediately see
results or outputs.
 5. Command History
 Stores a list of previously executed commands so users can reuse them by double-
clicking.
 6. Editor
 A separate window used to write, edit, and run scripts (.m files) and functions.
 7. Figure Window
 Opens automatically when plots or graphs are generated. It shows 2D and 3D ⭐
MATLAB Basic Syntax Uses
1. Semicolon ( ; )

 Purpose: To end a statement and suppress output in Command Window.


 Example:

a = 5; % No output displayed

b = 10; % No output displayed

 Without semicolon, MATLAB automatically displays the result.


3
2. Colon ( : )

 Purpose:
1. Create vectors with a range.
2. Access matrix rows or columns.
 Examples:

x = 1:5; % Creates [1 2 3 4 5]

y = 0:0.5:2; % Creates [0 0.5 1 1.5 2]

A = [1 2 3;4 5 6];

row1 = A(1,:); % Selects entire first row

col2 = A(:,2); % Selects entire second column

3. Adding Comments ( % )

 Purpose: To write explanations or notes in code; ignored by MATLAB.


 Example:

% This is a comment

a = 10; % Variable a is assigned 10

4. Strings / Text ( ' ' or " " )

 Purpose: To store text data or display messages.


 Examples:

name = 'Mujahid'; % Single quotes string

greeting = "Hello World"; % Double quotes string (recommended in newer MATLAB)

disp('Welcome to MATLAB'); % Display text


4
5. Combining Colon and Semicolon

 Useful for creating row/column vectors without printing each step:

v = 1:2:10; % Row vector [1 3 5 7 9]

MATLAB Commands and Their Purpose


1. clc
Definition:
clc is a MATLAB command used to clear the text displayed in the Command Window.
Purpose:
 Removes all previous outputs from the Command Window.
 Keeps workspace variables intact.

2. clear
Definition:
clear deletes all variables stored in the workspace.
Purpose:
 Frees memory by removing variables.
 Useful to start fresh without closing MATLAB.

3. clear variableName
Definition:
Deletes a specific variable from the workspace.
Purpose:
 Removes only selected variable(s) while keeping others.
 Example: clear a deletes variable a.

4. close all
5
Definition:
Closes all open figure windows in MATLAB.
Purpose:
 Clears all graphical windows.
 Useful before generating new plots to avoid overlapping figures.

5. pwd
Definition:
pwd displays the current working directory (folder) path in MATLAB.
Purpose:
 Helps know where MATLAB is currently reading or saving files.

6. cd foldername
Definition:
Changes the current working directory to the specified folder.
Purpose:
 Allows MATLAB to access files from a different folder.
 Example: cd 'C:\Users\Desktop'

7. save filename
Definition:
Saves all workspace variables into a .mat file.
Purpose:
 Stores data for future use.
 Example: save [Link]

8. load filename
Definition:
Loads saved variables from a .mat file into workspace.
6
Purpose:
 Retrieves previously saved data for analysis.
 Example: load [Link]

9. who
Definition:
Lists all variable names currently in the workspace.
Purpose:
 Quickly checks which variables are available.

10. whos
Definition:
Displays detailed information about all variables in the workspace, including size, class,
and bytes.
Purpose:
 Helps in understanding memory usage and variable types.

11. = (Assignment)
Definition:
The equals sign is used to assign a value to a variable.
Purpose:
 Stores numeric, string, or matrix data in a variable.
 Example: a = 5

12. : (Colon)
Definition:
Creates vectors with a specified range or selects rows/columns in a matrix.
Purpose:
 Generate sequences easily.
7
 Select specific rows/columns.
 Example: x = 1:5 → [1 2 3 4 5]

13. ; (Semicolon)
Definition:
Suppresses output in Command Window.
Purpose:
 Prevents printing the result of a statement.
 Example: a = 10;

14. disp()
Definition:
Displays the value of a variable or message in the Command Window.
Purpose:
 Shows results without semicolon suppression.
 Example: disp('Hello World')

15. ' ' or " " (String)


Definition:
Used to store textual data as a string.
Purpose:
 Represent words, sentences, or messages.
 Example: name = 'Mujahid';

16. + - * / ^ (Arithmetic Operators)


Definition:
Used for basic mathematical operations: addition, subtraction, multiplication, division, and
exponentiation.
Purpose:
8
 Perform calculations on numbers or matrices.
 Example: c = a + b

17. sqrt(x)
Definition:
Calculates the square root of a number or matrix elements.
Purpose:
 Mathematical computation.
 Example: sqrt(16) → 4

18. exp(x)
Definition:
Computes the exponential function e^x.
Purpose:
 Used in mathematical and engineering computations.
 Example: exp(1) → 2.7183

19. log(x)
Definition:
Computes the natural logarithm (base e) of x.
Purpose:
 Useful in scientific and mathematical calculations.
 Example: log(10)

20. log10(x)
Definition:
Computes the base-10 logarithm of x.
Purpose:
9
 Converts numbers to logarithmic scale.
 Example: log10(100) → 2

21. sin(x), cos(x), tan(x)


Definition:
Compute trigonometric functions in radians.
Purpose:
 Used in engineering, physics, and mathematics.
 Example: sin(pi/2) → 1

22. abs(x)
Definition:
Returns the absolute value of a number.
Purpose:
 Converts negative numbers to positive.
 Example: abs(-5) → 5

23. round(x), floor(x), ceil(x)


Definition:
 round(x) → rounds to nearest integer
 floor(x) → rounds down
 ceil(x) → rounds up
Purpose:
 Used for approximating numbers.
 Example: round(3.6) → 4, floor(3.6) → 3, ceil(3.1) → 4
10
⭐ What is MATLAB Plotting?
Definition:
MATLAB plotting is the process of creating 2D and 3D visual representations of data,
functions, or matrices using built-in MATLAB functions. Plotting allows users to analyze,
interpret, and present data graphically instead of using only numbers.

Purpose:

 To visualize data trends and patterns.


 To compare variables and functions.
 To create graphs, charts, and figures for reports, presentations, and research.
 Supports 2D plots (plot, bar, scatter) and 3D plots (surf, mesh, plot3).

Example:

x = 0:0.1:2*pi; % Create x values from 0 to 2*pi

y = sin(x); % Calculate y = sin(x)

plot(x, y) % Plot a 2D sine wave

title('Sine Wave') % Add title

xlabel('x-axis') % Label x-axis

ylabel('y-axis') % Label y-axis

grid on % Show grid

Output:
A smooth 2D sine wave graph with labeled axes, title, and grid lines.

2D Plotting in MATLAB
Definition:
2D plotting in MATLAB is the process of creating two-dimensional graphs to visualize the
relationship between two variables (x and y) on a flat plane. It is used for analyzing trends,
patterns, and comparisons in data.

Purpose:

 To display data on X and Y axes.


 Useful for line plots, bar charts, scatter plots, histograms, etc.
11
Example:

x = 0:0.1:2*pi; % X values

y = sin(x); % Y = sin(x)

plot(x, y) % Create 2D plot

title('2D Sine Wave')

xlabel('x-axis')

ylabel('y-axis')

grid on

Output:
A 2D sine wave graph showing y = sin(x) versus x.

3D Plotting in MATLAB
Definition:
3D plotting in MATLAB is the process of creating three-dimensional graphs to visualize
relationships among three variables (x, y, z) in space. It is used for surface, mesh, and
volumetric data visualization.

Purpose:

 To show data in X, Y, and Z axes.


 Useful for surfaces, 3D curves, mesh grids, and engineering modeling.

Example:

[x, y] = meshgrid(-2:0.2:2, -2:0.2:2); % Create X-Y grid

z = x.^2 + y.^2; % Z values

surf(x, y, z) % Create 3D surface plot

title('3D Surface Plot')

xlabel('X-axis')
12
ylabel('Y-axis')

zlabel('Z-axis')

Output:
A 3D surface plot representing z = x² + y², showing a paraboloid in space.

You might also like