Introduction to MATLAB
Introduction to MATLAB
Introduction to MATLAB:
Desktop Window: Desktop Window is the central interface for working with MATLAB. It
integrates several components to facilitate coding, data analysis, and visualization.
DIAGRAM
Command Window:
The main window where users enter commands and run scripts interactively.
Command History:
Displays previously executed commands, allowing for easy re-execution or editing.
Workspace Window:
Shows current variables in the workspace, along with their sizes, data types, and
values.
Editor Window:
A text editor for writing, editing, and saving MATLAB scripts or functions (.m files).
Figure Window:
Used for displaying plots and graphical outputs.
Current Folder Window:
Shows the files and folders in the current directory, helping manage the project
workspace.
Each window plays a specific role in MATLAB, aiding in code development, execution, and
visualization.
1(b)
ADVANTAGES OF MATLAB:
1. Easy Syntax
2. Extensive Libraries
3. Efficient Data Handling
4. Powerful Visualization
5. Platform Independence
6. Specialized Toolboxes
7. Simulink Support
8. Integration
Efficient Data Handling: Ideal for managing large datasets and matrices.
2a)
M-files (.m): These are script or function files that contain MATLAB code. Scripts execute a
series of commands, while functions can take inputs and return outputs.
MAT-files (.mat): These files store workspace variables in binary format. They are used to save
and load data easily.
FIG-files (.fig): These files store graphical user interface (GUI) figures created in MATLAB, such
as plots and other visual elements.
P-files (.p): These are protected or encoded versions of M-files, used to share code without
exposing the original source.
Mex-files (.mex): These files are compiled C, C++, or Fortran functions that are callable from
within MATLAB for performance improvements.
Each file type serves specific purposes for managing code, data, or visualizations within
MATLAB.
2b)
MATLAB provides a rich set of built-in functions that facilitate various tasks in numerical
computation, data analysis, visualization, and more.
1. Mathematical Functions
3. Statistical Functions
5. Visualization Functions
7. Logical Functions
8. Special Functions
script files are used for simple tasks and work within the base workspace
1. Creating a Script:
Open MATLAB and in the command window, click on the New Script option from the
toolbar.
This opens a blank editor where you can write your MATLAB code.
Example:
function files are designed for modular, reusable code that operates in its own
workspace.
Creating a Function:
In the MATLAB editor, click on New Function or use the New Script button and
manually define a function using the function keyword with input and output
arguments.
Example:
Save the file with the same name as the function (e.g., addNumbers.m).
Example:
4a
Basic Data Types in MATLAB:
In MATLAB data can be stored in different types, numeric, text, complex number, etc. To store
these data MATLAB has different classes which have various characteristics.
Arithmetic Operators:
Relational Operators:
Compare two values and return a logical true (1) or false (0).
Examples:
== (equal)
~= (not equal)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to).
Logical Operators:
Bitwise Operations:
Set Operations:
Arithmetic Operators:
Relational Operators:
Compare two values and return a logical true (1) or false (0).
Examples:
== (equal)
~= (not equal)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to).
Logical Operators:
Bitwise Operations:
Set Operations:
Perform operations on sets, such as union, intersection, and set difference.
Examples:
union(A, B) (combines unique elements from A and B)
intersect(A, B) (finds common elements)
setdiff(A, B) (elements in A not in B).
1. Arrays:
An array is a collection of elements (of the same type) organized in multiple
dimensions. In MATLAB, arrays can be one-dimensional (vectors) or two-dimensional
(matrices) and can even extend to higher dimensions.
Example: A 1D array: A = [1, 2, 3, 4].
2. Vectors:
A vector is a special case of an array that has only one dimension. It can be either a
row vector (1 × n) or a column vector (n × 1).
Example: Row vector: v = [1, 2, 3] and column vector: v = [1; 2; 3].
3. Matrices:
A matrix is a two-dimensional array consisting of rows and columns. All elements in a
matrix must be of the same data type.
Example: A 2x3 matrix: M = [1, 2, 3; 4, 5, 6].
Syntax:
if condition
end
example:
x = 10;
if x > 5
end
Definition: Executes one block of code if a condition is true and another if it is false.
Syntax:
if condition
% Code if condition is true
else
end
Example:
x = 3;
if x > 5
else
end
Definition: Checks multiple conditions in sequence, executing the block for the first true
condition.
Syntax:
matlab
Copy code
if condition1
elseif condition2
else
end
Example:
matlab
Copy code
x = 0;
if x > 0
disp('x is positive');
elseif x < 0
disp('x is negative');
else
disp('x is zero');
end