Lab 1
Lab 1
Introduction
*Item category
SP Sample or specimen
C Consumable Caution Statement (Optional)
CH Chemical
W Labware, glassware, tool, and Procedure
components
E Equipment Questions & Report Writing
S Software
Introduction to Matlab
UEMB3244 Medical Imaging
Practical 1
Chapter Outline
• Starting MATLAB
• Familiarization with MATLAB Desktop
• Using MATLAB as a Calculator
• Help features in MATLAB
• Elementary Math Built-in Functions
• Working with Variables
Starting MATLAB
Current MATLAB Desktop
Directory
Workspace
Command
Window
Command
History
Order of Precedence
• help function_name
• helpwin function_name
help mean
• doc function_name
Accessing the Help Browser Via the
Startup Menu
Help Browser
Creating Variables in MATLAB
Variable name Assign operator Value
>> x = 3;
>> y = 4i;
>> z = (x+y)*(x-y)
z=
25
>> t = 0.1
t=
0.1000
>> x = sin(3*t + pi/2)
x=
0.9553
Type of MATLAB Arrays
• Scalars - just single element arrays
a=[1]
» a=[1 2 3 4]
a = Entering a row
vector
1 2 3 4
» b=[1;2]
b = Entering a column
1 vector
2
» c=[1 2; 3 4]
c = Entering a
1 2 matrix
3 4
The Colon (:) Operator
» a=[1 2 3; 4 5 6; 7 8 9]
a=
1 2 3 a([1 2 3],3)
a(1:3,3)
4 5 6 a(:,3)
a(1,1)
7 8 9
a(2:3,1:2)
Boolean Indexing
Boolean Operators >> Mass = [-2 10 NaN 30 -11 Inf 31];
= = equal to >> each_pos = Mass>=0
each_pos =
> greater than 0 1 0 1 0 1 1
< less than >> all_pos = all(Mass>=0)
all_pos =
~ not
0
& and >> pos_fin = (Mass>=0)&(isfinite(Mass))
| or pos_fin =
0 1 0 1 0 0 1
isempty() >> good_mass = Mass(pos_fin)
isfinite(), etc. . . . good_mass =
10 30 31
any()
all()
1 = TRUE
0 = FALSE
>> bool_ops
Strings as Variables
- A string is an array of characters and can include letters, digits, other
symbols and spaces
- Created using single quote delimiter (')
How do I locate my
• The MATLAB Workspace Browser allows you to access variables created in
to the data or variables created in the current MATLAB?
MATLAB session.
• By selecting the Workspace tab or use the functions
who and whos.
• The MATLAB Array Editor is use to view and edit a
visual representation of variables in the workspace.
Workspace
Commands for Managing Variables
>> subplot(2,2,2)
>> x = 0:.1:2*pi;
>> plot(x,sin(x))
>> subplot(2,2,3)
>> x = 0:.1:2*pi;
>> plot(x,exp(-x),'r')
>> subplot(2,2,4)
>> subplotex >> plot(peaks)
Images
>> a = magic(4) Use row 2 of
a = colormap for
16 2 3 13 pixel (1,2)
5 11 10 8
9 7 6 12
4 14 15 1
>> image(a);
>> map = hsv(16)
map =
1.0000 0 0
1.0000 0.3750 0 Row 2
1.0000 0.7500 0 .....
>> colormap(map)
Built-in colormap
Colormap
Colormap Name
Name Color
Color Scale
Scale Old built-in
colormap
parula
parula
jet
jet
hsv
hsv
hot
hot
cool
cool
spring
spring
summer
summer
autumn
autumn
winter
winter
gray
gray
bone
bone
copper
copper
pink
pink
lines
lines
colorcube
colorcube
prism
prism
flag
flag
white
white
Example: Images and Colormaps
The MathWorks
function y = mymean(x)
%MYMEAN Average or mean value.
% For vectors, MYMEAN(x) returns the mean value.
Online Help
% For matrices, MYMEAN(x) is a row vector
% containing the mean value of each column.
[m,n] = size(x);
if m == 1
MATLAB
Code m = n;
end
y = sum(x)/m;
Sample Problem 4: Area of Triangle
The area, A, of a triangle with sides of length a, b and c is given by:
A = √ s (s – a)(s – b)(s – c) ,
where s = (a + b + c)/2.
Tips :
(a) Decide on the name of function. Ex: myarea.m
(b) First line of the file must have the format
function [list of outputs] = function_name (list of inputs)
(c) Document the function (using %)
(d) Finally, include the code that defines the function
Comparison between Script Files and
Function Files
Similarities and Differences between the two files:
Pay = t*h;
if t>40
Pay = Pay + (t-40)*.50*h
end
sprintf('The worker''s pay is RM %5.2f \r ',Pay);
Iterative Loops
• Similar to other programming languages
• for -- Repeats loop a set number of times (based on index)
• while -- Repeats loop until logical condition returns FALSE
• https://web.stanford.edu/class/ee254/software/getstart.pdf
• https://www.mathworks.com/help/matlab/images_btfntr_-1.html
(working with images in Matlab)