1 of 30

INTRODUCTION �TO� MATLAB

By

Dr. Izzad Ramli

2 of 30

CONTENTS

  1. Introduction to MATLAB
  2. Designing MATLAB GUI
  3. CNN Toolbox with MATLAB GUI

3 of 30

INTRODUCTION TO MATLAB

4 of 30

INTRODUCTION TO MATLAB

  • MATLAB Environment
  • Getting Help in MATLAB
  • Matrices in MATLAB
  • Basic Operations
  • Logical Operation
  • Programming in MATLAB
  • Visualizations and Graphics

5 of 30

WHAT IS MATLAB?

MATLAB = Matrix Laboratory

• “MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++ and Fortran.” (www.mathworks.com)

• MATLAB is an interactive, interpreted language that is designed for fast numerical matrix calculations

6 of 30

MATLAB ENVIRONMENT

To get a default layout, just click layout button and click default

7 of 30

MATLAB HELP

• MATLAB Help is an extremely powerful assistance to learning

MATLAB.

• Help not only contains the theoretical background, but also shows demos for implementation.

• MATLAB Help can be opened by using the HELP pull-down menu.

8 of 30

USEFUL COMMANDS

who

Current variables in the workspace

a=1; who;

save

Save workspace variables to *.mat file

save('value_a.mat', 'a');

save mysession

Save mysession.mat with all variables

b=2; c=3; save mysession

load

Load variables from *.mat file

load value_a.mat

clear

Clear workspace variables

clear

close all

Close all open figures

figure('Name','Title');

close all

clc

Clear command window screen

clc

9 of 30

MATLAB HELP

10 of 30

MATRICES IN MATLAB

Matrix is the main MATLAB data type.

How to build a matrix?

Example

% creates 3 x 3 matrix

A = [10 20 30; 40 50 60; 70 80 90];

% creates a column vector

B = [1; 2; 3];

% creates a row vector

C = 1:10;

11 of 30

BASIC OPERATIONS

Special matrices:

– zeros(n,m), ones(n,m), eye(n,m), rand(n,m), magic(m)

Basic Operations defined on matrices:

+,-,*,/,^,’,sqrt,sin,cos, etc.

Example

zeros(2,2)

ones(4,1)

eye(4,4)

rand(1,10)

magic(4)

Y = sqrt(4)

12 of 30

LOGICAL OPERATIONS

Logical operators

==, <, >, (not equal) ~=, (not) ~

  • Returns TRUE (1) or (FALSE) 0

find(‘condition’) – Returns indexes of A’s

elements that satisfy the condition

Example

A = [1 12 18 7 9 11 2 15]

A > 10

A = [1 12 18 7 9 11 2 15]

A >= 2

A = [1 12 18 7 9 11 2 15]

A ~= 15

13 of 30

PROGRAMMING IN MATLAB

Variable naming rules:-

◦ Must be unique in the first 63 characters.

◦ Must begin with a letter.

◦ May not contain blank spaces or other types of punctuation.

◦ May contain any combination of letters, digits and underscores

◦ Case-sensitive

◦ Should not use MATLAB keyword

Example

1a = 2

a a = 2

test1 = 2

Test = 2

Absdasdasdadasdddadsaddhsadhsadihdshdsiahdsaidhashdasoidhadasidasdasd = 2

14 of 30

PROGRAMMING IN MATLAB

Scripts and functions

Script Files

◦ List of commands to be executed sequentially. Useful when same sequence is executed many times. They should be saved with extension script .m and should be placed on work path to be called.

>> script - runs the sequence

Functions

◦ Similar to script, but takes arguments

◦ A funcname.m file starts with:

function [output]=funcname(input)

◦ Any function

15 of 30

PROGRAMMING IN MATLAB

Scripts and functions (cont.)

A function, stat.m, that calculates the mean and standard deviation of a vector.

Example

% Create script stat.m

function[mean, stdev] = stat(x)

%stat, simple vector statistics

n=length(x);

mean=sum(x)/n

stdev=sqrt(sum((x-mean).^2/n))

To execute it:

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

stat(x)

16 of 30

PROGRAMMING IN MATLAB

Flow control

• MATLAB has five flow control

constructs:

– if statement

– switch statement

– for loop

– while loop

– break statement

17 of 30

PROGRAMMING IN MATLAB

IF

The general form of the IF statement is

IF expression statements

ELSEIF expression

statements

ELSE

statements

END

Example

attendance = 0.9;

grade_average = 70;

if ((attendance >= 0.90) &&

(grade_average >= 60))

pass = 1

end;

18 of 30

PROGRAMMING IN MATLAB

SWITCH

Switch among several cases based on expression

SWITCH switch_expr

CASE case_expr,

statement, …, statement

CASE {case_expr1, case_expr2,

case_expr3, …}

statement, …, statement

OTHERWISE

statement, …, statement

END

Example

method = 'Bilinear';

switch lower(method)

case {'bilinear'}

disp('Method is linear')

case 'cubic'

disp('Method is cubic')

case 'neares'

disp('Method is nearest')

otherwise

disp('Unknown method.')

end

19 of 30

PROGRAMMING IN MATLAB

for

• FOR repeats statements a

specific number of times

• The general form of a FOR

statement is:

fOR variable = expr

statements

END

Example

A = [3 6 9 4 1];

for i = 1:length(A)

disp(A(i))

end

20 of 30

VISUALISATIONS

Visualizations and graphics

plot(x,y)– plots 2D line

figure, figure(k) – open a new figure

subplot(3,1,2) – locate several plots in figure

hold on, hold off – refreshing

title(‘figure titile’) – add title to figure

axis([xmin xmax ymin ymax]) – change axes

mesh(x_ax,y_ax,z_mat) – view surface

contour(z_mat) – view z as topo map

Example

% Line plot

x=0:0.05:5;

y=sin(x.^2);

plot(x,y);

xlabel('Time')

ylabel('Amplitude')

21 of 30

VISUALISATIONS

Visualizations and graphics

plot(x,y)– plots 2D line

figure, figure(k) – open a new figure

subplot(3,1,2) – locate several plots in figure

hold on, hold off – refreshing

title(‘figure titile’) – add title to figure

axis([xmin xmax ymin ymax]) – change axes

mesh(x_ax,y_ax,z_mat) – view surface

contour(z_mat) – view z as topo map

Example

t = 1:0.01:10;

x1 = sin(2*pi*t);

x2 = sin(2*pi*t - 10);

figure

plot(t,x1,'Color','r')

figure

plot(t,x2,'Color','b')

22 of 30

VISUALISATIONS

Visualizations and graphics

plot(x,y)– plots 2D line

figure, figure(k) – open a new figure

subplot(2,2,2) – locate several plots in figure

hold on, hold off – refreshing

title(‘figure titile’) – add title to figure

axis([xmin xmax ymin ymax]) – change axes

mesh(x_ax,y_ax,z_mat) – view surface

contour(z_mat) – view z as topo map

Example

subplot(3,1,1)

x = linspace(0,10);

y1 = sin(x);

plot(x,y1)

title('Subplot 1: sin(x)')

subplot(3,1,2)

y2 = sin(2*x);

plot(x,y2)

title('Subplot 2: sin(2x)')

subplot(3,1,3)

y3 = sin(4*x);

plot(x,y3)

title('Subplot 3: sin(4x)')

23 of 30

VISUALISATIONS

Visualizations and graphics

plot(x,y)– plots 2D line

figure, figure(k) – open a new figure

subplot(3,1,2) – locate several plots in figure

hold on, hold off – refreshing

title(‘figure titile’) – add title to figure

axis([xmin xmax ymin ymax]) – change axes

mesh(x_ax,y_ax,z_mat) – view surface

contour(z_mat) – view z as topo map

Example

% Line plot

x=0:0.05:5;

y=sin(x.^2);

plot(x,y);

xlabel('Time')

ylabel('Amplitude')

str = sprintf('Time vs Amplitude’);

title(str)

24 of 30

DESIGNING MATLAB GUI

25 of 30

MATLAB GUI

Graphical User Interface Development Environment (GUIDE)

26 of 30

MATLAB GUI

3 Essential Characteristics

Components –

◦ Graphical Components

pushbuttons, edit boxes, sliders, labels, menus, etc…

◦ Static Components

Frames, text strings,…

◦ Both are created using the function uicontrol

Figures – components are contained in figures

Callbacks – The functions which perform the required action for the components.

27 of 30

MATLAB GUI

  • Text field & button
  • Slider
  • Pop-up menu
  • Plotting data to Axes
  • Insert image to Axes
  • Example of MATLAB GUI Project

28 of 30

MATLAB GUI

Please go to this link:

29 of 30

CNN TOOLBOX WITH MATLAB GUI

30 of 30

CNN TOOLBOX WITH MATLAB GUI

Please go to this link: