introduction to matlab lecture 3 of 4
TRANSCRIPT
![Page 1: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/1.jpg)
![Page 2: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/2.jpg)
>> A = 0:2:10A =
0 2 4 6 8 10>> B(1:6) = 2.5
B =2.5000 2.5000 2.5000 2.5000 2.5000 2.5000
>> C = A+BC =
2.5000 4.5000 6.5000 8.5000 10.5000 12.5000>> D = A.*B
D =0 5 10 15 20 25
>> A(1,3) = 8A =
0 2 8 6 8 10>> E = A./B
E =0 0.8000 3.2000 2.4000 3.2000 4.0000
2
![Page 3: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/3.jpg)
>> sum(A)
ans =
12.1000
>> mean(A)
ans =
2.0167
>> max(A)
ans =
8.9000
>> fix(A)
ans =
1 2 8 -8 4 3
3
![Page 4: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/4.jpg)
>> A(:,6) = 0.7A =
1.0000 2.7000 8.9000 -8.0000 4.0000 0.70006.5000 8.9000 5.0000 -0.9000 3.0000 0.7000
>> A = A(:,[1 3 4 5 6])A =
1.0000 8.9000 -8.0000 4.0000 0.70006.5000 5.0000 -0.9000 3.0000 0.7000
>> mean(A)ans =
3.7500 6.9500 -4.4500 3.5000 0.7000
>> mean(mean(A))ans =
2.0900
![Page 5: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/5.jpg)
Matlab string variables› Operations
› Built-in functions
Flow control› Conditional
› Iterations
Data transport: › Importing into the workspace
› Exporting from the workspace
5
![Page 6: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/6.jpg)
MATLAB also can accept and manipulate string variables.
A string is defined by enclosing it in single quotes.
Example: aString = ‘Hello World!’
6
![Page 7: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/7.jpg)
To convert a string to lowercase, use the lower command.
Example:change string in matrix A to lowercase:
B = lower(A)
7
![Page 8: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/8.jpg)
To convert a string to uppercase, use the upper command.
Example:change string in matrix A to uppercase:
B = upper(A)
8
![Page 9: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/9.jpg)
Concatenating string means merging two or more strings together.
Example: to concatenate str1 and str2:
newStr = strcat(str1,str2)
9
![Page 10: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/10.jpg)
To replace part of the string with a new value, use the strrep command.
Example: replace the word ‘lama’ with the word ‘baru’ in the string str1.
strrep(str1,’lama’,’baru’)
10
![Page 11: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/11.jpg)
Create a vertical array of strings.
>> C = strvcat('Hello','Yes','No','Goodbye')
>> C =
Hello
Yes
No
Goodbye
11
Detect space characters in an array
>> isspace(' Find spa ces ')
>> Columns 1 through 13
1 1 0 0 0 0 1 0 0 0 1 0 0
Columns 14 through 15
0 1
![Page 12: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/12.jpg)
Use strfind to find a two-letter pattern in string S:
>> S = 'Find the starting indices of the pattern string';
>> strfind(S, 'in')
>> ans =
2 15 19 45
>> strfind(S, 'In')
>> ans =
[]
>> strfind(S, ' ')
>> ans =
5 9 18 26 29 33 41
12
![Page 13: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/13.jpg)
Findstr finds a string within another, longer string: k = findstr(str1,str2)
The search performed by findstr is case sensitive. Any leading and trailing blanks in either input string are explicitly included in the comparison.
Unlike the strfind function, the order of the input arguments to findstr is not important. This can be useful if you are not certain which of the two input strings is the longer one.
>> s = 'Find the starting indices of the shorter string.';
>> findstr(s,'the')
>> ans =
6 30
>> findstr('the',s)
>> ans =
6 30
13
![Page 14: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/14.jpg)
Strmatch finds possible matches for a string
The statement
>> x = strmatch('max', strvcat('max', 'minimax', 'maximum'))
returns x = [1; 3] since rows 1 and 3 begin with 'max'.
The statement
>> x = strmatch('max', strvcat('max', 'minimax', 'maximum'),'exact')
returns x = 1, since only row 1 matches 'max' exactly.
14
![Page 15: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/15.jpg)
strncmp compares the first n characters of two strings
strncmp is case sensitive. Any leading and trailing blanks in either of the strings are explicitly included in the comparison.
strncmp is intended for comparison of character data. When used to compare numeric data, strncmp returns 0.
strncmpi compares first n characters of strings ignoring case
15
![Page 16: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/16.jpg)
strtrim removes leading and trailing white-space
from string
>> str = sprintf(' \t Remove leading white-
space')
>> str =
Remove leading white-space
>> str = strtrim(str)
>> str =
Remove leading white-space
16
![Page 17: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/17.jpg)
str2num used for String to number conversion
>> str2num('3.14159e0') is approximately .
To convert a string matrix:
>> str2num(['1 2';'3 4'])
>> ans =
1 2
3 4
num2str used for number to string conversion
17
![Page 18: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/18.jpg)
int2str used for Integer to string conversion
>> int2str(2+3) is the string '5'.
One way to label a plot is:
>> title(['case number ' int2str(n)])
For matrix or vector inputs, int2str returns a string matrix:
>>int2str(eye(3))
>> ans =
1 0 0
0 1 0
0 0 1
18
![Page 19: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/19.jpg)
char(X) can be used to convert an array that contains positive integers representing numeric codes into a MATLAB character array.
To print a 3-by-32 display of the printable ASCII characters:
>> ascii = char(reshape(32:127,32,3)')
>> ascii =! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _
' a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
double(S) converts the string to its equivalent double-precision numeric codes.
19
![Page 20: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/20.jpg)
str=‘012ABCabc’
num=double(str)=[48 49 50 65 66 67
97 98 99]
char(num) returns ‘012ABCabc’
Strings also can undergo arithmetic operations as they are dealt by their ASCII codes
20
![Page 21: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/21.jpg)
If…else Operator The if…else
operator tests a condition.
If the condition is true, then execute the if block.
If the condition is false, execute the else block.
21
if (condition)
% if block
else
% else block
end
% conditions that can be tested
% == : is equal to
% ~= : is not equal to
% > : larger than
% >= : larger than or equal
% <= : less than or equal
% < : less than
![Page 22: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/22.jpg)
Example:
clear, close allclc
x = 3;
if (x > 5)disp('The number is more than 5.')
elseif (x == 5)disp('The number is equal to 5.')
elsedisp('The number is less than 5.')
end
22
![Page 23: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/23.jpg)
For loop Used to repeat a set of statements multiple
times.
The for loop format is:
for(startingvalue:increment:endingvalue)
23
clear, close all
clc
% i is the value of the counter
for i = initial_value:increment:ending_value
% statements in this block will be executed until i
% reaches the ending_value
end
![Page 24: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/24.jpg)
Example:
clear, close all
clc
for i = 1:1:15
st1 = strcat('The value of iinside the loop is: ',int2str(i));
disp(st1)
end
24
![Page 25: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/25.jpg)
While loop Used to repeat a
set of statements while the tested condition is true.
The while loop format is:
while(condition)
The tested condition is the same as if…else
25
% conditions that can be
tested
% == :is equal to
% ~= :is not equal to
% > :larger than
% >= :larger than or equal
% <= :less than or equal
% < :less than
![Page 26: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/26.jpg)
Example:
clear, close allclc
counter = 1;
while(counter <= 15)st1 = strcat('The value of i inside
the loop is: ',int2str(counter));disp(st1)counter = counter + 1;
end
26
![Page 27: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/27.jpg)
switch
switch {expression}case {value1}
{command1; command2; ...}
case {value2}{command1; command2; ...}
otherwise{command1; command2; ...}
end
expression evaluation can be to numeric or string case can be single values or vectors of values
Execution is terminated after first satisfied expression
27
![Page 28: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/28.jpg)
File > Import Data % from the navigation bar
>> uiimport <filename>
Example: >> uiimport planetsize.txt
>> dlmread('filename', '<delimiter>')
Example: >> planets2 = dlmread('planets2.txt', ';')
>>load 'filename'
Example: >> load 'planets3.txt'
28
![Page 29: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/29.jpg)
>> xlsread: Read files from ExcelExample: >> planets6 = xlsread('planets6.xls')
>> imread: Read graphics file (several formats) Example: >> planets7 = imread('planets7.jpg');
creates the matrix variable planets7
view with: >> imshow(planets7)
Other special read functions› aviread [avi audio/visual files]
› textread [read from text file]
› fscanf [read by format, similar to C language function]
29
![Page 30: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/30.jpg)
diary: text file of command window output
>> diary <filename.txt>
….
>> diary off
save: save workspace objects or text to disk>> save <filename>
Binary file <filename>.mat
>> save <filename>.txt <variable> –ascii -tabs
Text file <filename>.txt
Matrix column elements separated by tabs with -tabs
30
![Page 31: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/31.jpg)
Sava data in files: >> save myfile VAR1 VAR2 …
or
>> save(‘myfile’,’VAR1’,’var2’)
File Formats:
› mat -> Binary MAT-file form
› ascii -> 8-digit ASCII form
› ascii–tabs Delimit array elements with tabs
31
![Page 32: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/32.jpg)
Read tables of ASCII data with load
Other functions like textread will read simple files
Sometimes, you’ve just got to do it yourself (Complicated files)
To read a file manually, open with fopen› fid=fopen(‘fname’, ‘rt’);
› fid will be <1 if open fails
› File I/O functions accept fid
› Close the file when you’re done with fclose(fid)
32
![Page 33: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/33.jpg)
A=fscanf(fid,cstring,{N})
› like C’s fscanf, cstring is a C format string:
‘%d\t%f’--integer (%d),tab(\t),double (%f)
lin=fgetl(fid)
› Reads a single line from the file as text (char
array)
› Process lin with str2num, findstr, sscanf
Test for end of file with feof(fid);
33
![Page 34: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/34.jpg)
Save matrices using save fname varnameascii
Doing it yourself:› fid=fopen(‘fname’,’wt’)
› fprintf(fid,cstring, variables)
› Example:A=[(1:10)’, sin(2*pi*0.1*(1:10)’)];%[integers,
doubles]
fid=fopen(‘example.txt’,’wt’);
fprintf(fid,’%d %f\n’,A’);
fclose(fid);
34
![Page 35: Introduction to matlab lecture 3 of 4](https://reader033.vdocuments.us/reader033/viewer/2022042511/55a68cbc1a28abc57d8b4643/html5/thumbnails/35.jpg)
Save some images on your desk with sequential names:
E.g. 1.jpg-2.jpg-3.jpg etc
Im1.tif-Im2.tif-Im3.tif etc
Image1.bmp-Image2.bmp-Image3.bmp etc
Write a program to do the following:1. Compose the image file path
2. Read the image content
3. Find the image mean value
4. Store the means in a text file ‘MEANS.mat’