You are on page 1of 38

MATLAB MODULE

1st MODULE
Created by:
Ahmad Kanzu Syauqi Firdaus, S.Si
1

TABLE OF CONTENT

CHAPTER I INTRODUCTION ............................................................................................ 3


1.1. MATLAB (Matrix Laboratory) ................................................................................... 3
1.2. Base Layout ................................................................................................................ 3
1.3. Basic Math Operation .................................................................................................. 5
CHAPTER II VARIABLE AND OPERATOR ...................................................................... 6
2.1. Variable ...................................................................................................................... 6
2.2. Operator ...................................................................................................................... 7
CHAPTER III M-FILE .......................................................................................................... 8
CHAPTER IV ARRAY ....................................................................................................... 10
4.1. Making Array ............................................................................................................ 10
4.2. Special Array ............................................................................................................ 11
4.3. Accessing Part of Array’s Element ............................................................................ 11
4.4. Adding Array’s Element............................................................................................ 12
4.5. Removing Array’s Element ....................................................................................... 12
4.6. Updating Array’s Element ......................................................................................... 13
CHAPTER V MATRICES................................................................................................... 14
5.1. Making Matrices ....................................................................................................... 14
5.2. Special Matrices ........................................................................................................ 14
5.3. Accessing Part of Matrix’s Element........................................................................... 16
5.4. Combining Matrices .................................................................................................. 16
5.5. Removing Part of Matrix Columns or Rows .............................................................. 17
5.6. Updating Matrix’s Element ....................................................................................... 18
CHAPTER VI STATEMENT CONTROL ........................................................................... 19
6.1. If ............................................................................................................................... 19
6.2. While ........................................................................................................................ 19
6.3. For ............................................................................................................................ 20
6.4. Continue.................................................................................................................... 20
6.5. Break ........................................................................................................................ 21
6.6. Return ....................................................................................................................... 21
CHAPTER VII CELL ......................................................................................................... 22
7.1. Making Cell .............................................................................................................. 22
7.2. Accessing Part of Cells Elemen ................................................................................. 23
7.3. Combining Cell ......................................................................................................... 24
7.4. Removing Part of Cells’ Element .............................................................................. 24
7.5. Updating Cells’ Element ........................................................................................... 25
2

CHAPTER VIII STRUCT ................................................................................................... 27


8.1. Making Struct............................................................................................................ 27
8.2. Accessing Part of Struct’s Elemenet .......................................................................... 28
CHAPTER IX INPUTTING DATA..................................................................................... 29
9.1. Inputting Data Manually from Command Window .................................................... 29
9.2. Inputting Data by M-File ........................................................................................... 29
9.3. Inputting Data from Excel File (*.xls, *.xlsx, *.csv) .................................................. 35
9.4. Inputing Data from Sound File .................................................................................. 35
3

CHAPTER I
INTRODUCTION

1.1. MATLAB (Matrix Laboratory)


 High level programming language (without declaration)
 Including built-in functions
 Equipped with toolbox for certain application
 Can be used for:
o Mathematic and computation
o Build algorithm
o Data acquisition (can be integrated with some hardware)
o Modeling, simulating, prototype
o Analyzing, exploring, and visualizing data
o Interpolating (graph)
 MATLAB ≠ MATLAB Simulink

1.2. Base Layout

Command Window: place where all MATLAB commands are written and executed.
Workspace: consist of information about variables inside MATLAB memory.
4

Command History: information about any commands that had been write before.

MATLAB layout can be arranged by click “Layout” button.

In MATLAB 2013 or newer version there is “PLOTS” tab which consist of some graph
choice. When cursor directed to one graph, there will be shown the function which is used to
show that graph.
5

In MATLAB 2013 or newer version, there is “APPS” tab which consist of some built-in tool
which can be useful to solve some cases for someone who require.

1.3. Basic Math Operation


6

CHAPTER II
VARIABLE AND OPERATOR

2.1. Variable
2.1.1. Special Variables

Variable Value
ans Any results
pi Comparison between circle circumference with its
diameter
eps Smallest number so that if it is added by one, the
result is a number that greater than one in the
computer
flops The number of floating point operation
inf Infinity
NaN Not number
i and j Imaginer number
nargin The number of function's input argument
nargout The number of function's output argument
realmin The smallest real positive number that can be used
realmax The largest real positive number that can be used
tic dan toc tic = starting point of computation time counter,
toc = stopping point of computation time counter

2.1.2. Making Variable


 No need declaration
 Case sensitive
 Must be begun by alphabet. Combination of alphabet, number, and “ _ “ sign can be
applied.
Example: A=1; A1=2; A_1=3; a=4; a1=5; a_1=6;
 Maximum character length of the variable is 31 characters, after that, it will be
ignored.
7

2.2. Operator
2.2.1. Arithmetic Operator
Operation Operator
Increment +
Decrement -
Matrix rule multiplication *
Matrix element multiplication .*
Matrix rule right division /
Matrix element right division
Matrix rule left division \
Matrix element left division .\
Degree (matrix rule) ^
Degree for matrix element .^
Step :

2.2.2. Relational Operator


Operation Operator
Equal ==
Not Equal ~=
Less than <
More than >
Less than and equal <=
More than and equal >=
And &&
Or ||

2.2.3. Logical Operator


Operation Explanation Operator
AND value is 1 if both of elements are &
true, and value is 0 for others
OR value is 1 if one of its element is |
true
Negation value is 1 if element value is 0, ~
and value is 0 if element is not
equal 0
XOR xor()
value is 1 if one of both of
elements have different value,
and value is 0 if both elements
have the same value
8

CHAPTER III
M-FILE

M-file is some MATLAB commands that are written chronologically and saved as a
file. The saved filename will have “*.m” extension which indicate that the file is MATLAB
file. M-file can be written either as a script or a function that receive argument or input which
produce output.

Same as another program, the fundamental principle of MATLAB can be drawn as following:

PROCESS
9

Example:

Results:
10

CHAPTER IV
ARRAY

Array is the association of variables which have the same data type and are expressed
with the same name.

4.1. Making Array


11

4.2. Special Array


4.2.1. Randperm

4.2.2. Linspace

4.3. Accessing Part of Array’s Element


12

4.4. Adding Array’s Element

4.5. Removing Array’s Element


13

4.6. Updating Array’s Element


14

CHAPTER V
MATRICES

5.1. Making Matrices

5.2. Special Matrices


5.2.1. Zeroes
15

5.2.2. Ones

5.2.3. Identity Matrix

5.2.4. Magic Square


Matriks yang memiliki Results jumlah yang sama pada elemenelemen
baris, kolom dan diagonalnya.

5.2.5. Random Matrix


16

5.3. Accessing Part of Matrix’s Element

5.4. Combining Matrices


17

Other example:

5.5. Removing Part of Matrix Columns or Rows


18

5.6. Updating Matrix’s Element


19

CHAPTER VI
STATEMENT CONTROL

6.1. If
“If” is statement control that is used to evaluate logical expression and execute amount
statement which based on expression value. If else can be written as follows:

if <logical expression> statement


else if < logical expression > statement
else< logical expression > statement
end

example:

6.2. While
While is used to do looping process during expression condition is satiable. When the
condition is insatiable, the looping process will be stopped directly. The common form of
while is:
while <expression>
Statement
end
20

Example:

6.3. For
For is used to do looping process during the expression condition is satiable. The difference
between for and while is in for, the number of looping can be known, whereas in while is
depend on expression value. The common form of for is:

for index=first value:step:end value


statement
end

Example:

6.4. Continue
Continu is continuing to the next iteration without doing statement under the continue
condition.
21

Example:

6.5. Break
Break is used to stop the looping process without continuing the loop. The program codes
after iteration are still executed by MATLAB. Example:

6.6. Return
Return is used to stop the looping process without continuing the loop. The program codes
after iteration are not executed by MATLAB. Example:
22

CHAPTER VII
CELL

7.1. Making Cell


23

7.2. Accessing Part of Cells Elemen


Accessing one of cell’s elements is by braces “ { } ”

If accessing some cell’s elements by braces “{ }”, the result is:

If accessing some cell’s elements by brackets “( )”, the result is:


24

7.3. Combining Cell

7.4. Removing Part of Cells’ Element


To remove cell’s element one by one, use braces “{ }”
25

To remove some rows or columns of cell, use brackets “( )”

7.5. Updating Cells’ Element


To update every one element, use braces “{ }”
26

To update rows / columns of cell, use brackets “( )”


27

CHAPTER VIII
STRUCT

8.1. Making Struct


Insert the following program code to the M-File:
% generate struct
clear all
close all
clc

%% Generate Struct Spaces


Data=struct('Class',[],'Name',[],'ToeflScore',[]);

%% Filling Struct
%Filling Class
Data(1).Class='A';
Data(2).Class='B';
Data(3).Class='B';

%Filling Name
A_Class{1,1}='Farah';
A_Class{2,1}='Jack';
A_Class{3,1}='Yuki';

B_Class{1,1}='Yuma';
B_Class{2,1}='Hogan';
B_Class{3,1}='Gerrard';

C_Class{1,1}='Ahmed';
C_Class{2,1}='Eto';
C_Class{3,1}='Bruno';

Data(1).Name=A_Class;
Data(2).Name=A_Class;
Data(3).Name=A_Class;

%Filling ToeflScore
A_Toefl(1,1)=600;
A_Toefl(2,1)=475;
A_Toefl(3,1)=500;

B_Toefl(1,1)=420;
B_Toefl(2,1)=512;
B_Toefl(3,1)=590;

C_Toefl(1,1)=595;
C_Toefl(2,1)=575;
C_Toefl(3,1)=530;

Data(1).ToeflScore=A_Toefl;
Data(2).ToeflScore=A_Toefl;
Data(3).ToeflScore=A_Toefl;
28

8.2. Accessing Part of Struct’s Elemenet


29

CHAPTER IX
INPUTTING DATA

9.1. Inputting Data Manually from Command Window


Make sure the data is exist in folder: C:\Users\(Your Computer Name)\Documents\MATLAB

9.2. Inputting Data by M-File


9.2.1. Data Is Gathered in One Folder with M-File
Make sure that data and M-File are placed in one folder
30

Program code:

Click “Run” button, so that:

Select “Add to Path”, so the results:


31

9.2.2. Data Is Placed Outside from Default Folder and Is Not The Same Folder As With
M-File
 Using “Set Path”
Pastikan terlebih dahulu letak file yang ingin diakses

Click Set Path

Click “Add Folder”


32

Click “Select Folder”

Click “Save”, then Click “Close”


33

1 2

Program code:

Results:
34

 Using Program Code


Insert this program code as following:
following

After clicking “Run”:


35

Results:

9.3. Inputting Data from Excel File (*.xls, *.xlsx, *.csv)


Data Type Function
*.xls, *.xlsx, *.csv xlsread( )
*.csv csvread( )

9.4. Inputing Data from Sound File


Insert this following program code into M-File:

(Note: to read *.wav file, the “audioread” function is can be replaced by “wavread”
36

The Results Example:

To play the sound, use this following code:


sound(S,FS)

You might also like