Lecture 7 - Cells Structures and Files
Lecture 7 - Cells Structures and Files
Lecture 7:
Recursion + Structures, cells and files
Recursion
2
Recursion
Recursion
What is a recursion?
A recursive definition is when something is defined in
terms of itself
of itself
Recursion
function babushka()
babushka();
disp('B');
>> babushka
How many times will the function print the letter B?
function babushka()
babushka();
function babushka()
disp('B');
babushka();
function babushka()
disp('B');
babushka();
disp('B');
Recursion
Recursion
function babushka()
disp('B');
babushka();
>> babushka
How many times will the function print the letter B?
function babushka()
disp('B');
babushka();
function babushka()
disp('B');
babushka();
function babushka()
disp('B');
babushka();
Recursion
function babushka(i)
if i < 1
return ;
end
babushka(i - 1);
disp(i);
A stopping condition
>> babushka(3)
Recursion
function babushka(i)
3
if i < 1
return ;
end
babushka(i - 1);
function babushka(i)
disp(i);
if i < 1
return ;
end
babushka(i - 1);
disp(i);
function babushka(i)
1
if i < 1
return ;
end
babushka(i - 1);
function babushka(i)
disp(i);
if i < 1
return ;
end
babushka(i - 1);
disp(i);
9
Recursion
function babushka(i)
if i < 1
return ;
end
disp(i);
babushka(i - 1);
>> babushka(3)
10
Factorial
n! = n * (n 1) * (n 2) * 1
11
Factorial
function
if n <= 1
n_fac = 1;
return;
end
n_fac = n * facto (n - 1);
A stopping condition
12
n=0
n=1
n=2
n=3
n=5
13
if index < 0
vec = [];
elseif index == 1
vec = [1];
else
vec = [1 1]; prev = 1; curr = 1;
for i = 2 : index
next = curr + prev;
prev = curr;
curr = next;
vec = [vec, next];
end
end
disp(vec);
14
Fibonacci
A stopping condition
Fib(n)
if index < 0
vec = [];
elseif index == 1
vec = [1];
else
vec = [1 1];
prev = 1;
curr = 1;
for i = 2 : index
next = curr + prev;
prev = curr;
curr = next;
vec = [vec, next];
end
end
disp(vec);
17
La-cucaracha
La-cucaracha
K
possibilities
Total_num
1,1
2
1,1,1
2,1
1,2
4
1,1,1,1
2,1,1
1,2,1
1,1,2
2,2
19
20
x = [ 1,
4,
y = [shusha]
5,
2,
-1]
21
22
rembrant
van-gogh
leonardo
michael
23
>> x(1,
>> x(1,
>> x(2,
>> x(2,
>> x
x =
1)
2)
1)
2)
=
=
=
=
{
{
{
{
[1 2 3; 4 5 6; 7 8 9] };
2.5 };
'bababluba' };
[10 : -2 : 0] };
[3x3 double]
'bababluba'
[
2.5000]
[1x6 double]
x{1,
x{1,
x{2,
x{2,
1}
2}
1}
2}
=
=
=
=
[1 2 3; 4 5 6; 7 8 9] ;
2.5 ;
'bababluba' ;
[10 : -2 : 0] ;
24
x(i, j) = { y }
and
x{i, j} = ( y )
have the same outcome.
Both store the content of the variable y inside the (i, j) element of cell
array x.
25
Example:
>> x = 1;
>> x(1, 1) = {1 : 4}
??? Subscripted assignment dimension mismatch.
26
x =
[3x3 double]
'bababluba'
[
2.5000]
[1x6 double]
[4x4 double]
'shusha'
[
4]
[1x4 double]
y =
>>z = [x; y]
[3x3 double]
'bababluba'
[4x4 double]
'shusha'
[
[1x6
[
[1x4
2.5000]
double]
4]
double]
27
2.5000]
4]
>>size(a)
ans =
28
29
30
x(1,
x(1,
x(2,
x(2,
1)
2)
1)
2)
=
=
=
=
{
{
{
{
[1 2 3; 4 5 6; 7 8 9] };
'bababluba' };
[1 : 10] };
'A' };
>> b = x{1, 1}
b =
1
2
4
5
7
8
>> c = x{1, 2}
c =
bababluba
3
6
9
>> class(b)
double
>> class(c)
char
31
x(1,
x(1,
x(2,
x(2,
1)
2)
1)
2)
=
=
=
=
{
{
{
{
[1 2 3; 4 5 6; 7 8 9] };
'bababluba' };
[1 : 10] };
'A' };
>> b = x(1, 1)
b =
[3x3 double]
>> c = x(1, 2)
c =
'bababluba'
>> class(b)
cell
>> class(c)
cell
32
33
Structures
34
Structures creation
Structure name
Structure field
>> dogs.name
>> dogs.breed
>> dogs.age
>> dogs.special_food
>> dogs
dogs =
name:
breed:
age:
special_food:
= 'rufus';
= 'Bulldog';
= 1.5;
% in years
= 'none';
'rufus'
'Bulldog '
1.5000
'none'
35
Array of structures
>>
>>
>>
>>
dogs(2).name
dogs(2).breed
dogs(2).age
dogs(2).special_food
=
=
=
=
'kin-kong';
'Chihuahua';
5;
'filet mignon';
>>
>>
>>
>>
dogs(3).name
dogs(3).breed
dogs(3).age
dogs(3).special_food
=
=
=
=
'wong';
'pekingese';
20;
'sushi';
>> dogs =
1x3 struct array with fields:
name
breed
age
special_food
36
>> x = dogs(2)
x =
name:
breed:
age:
special_food:
'kin-kong'
'Chihuahua'
5
'filet mignon'
37
>> x.breed
ans =
Chihuahua
>> dogs(2).breed
ans =
Chihuahua
38
>> dogs.age
ans =
1.5000
ans =
5
ans =
20
5.0000
20.0000
39
>> dogs.name
ans = rufus
ans = kin-kong
ans = Wong
>> names = dogs.name
??? Illegal right hand side in assignment. Too many elements.
'wong'
A better solution is to
convert the list into a
cell array of strings 40
We are going to build a program for managing a five star hotel for
dogs
The program will manage the hotel log and enable:
41
42
44
+ 1).name
= input('Enter dog name: ', 's');
+ 1).breed
= input('Enter dog breed: ', 's');
+ 1).age
= input('Enter dog age: ');
+ 1).special_food = input('Enter dog special food: ', 's');
+ 1).days_in_hotel = input('Enter number of days dog will ...
hotel: ');
+ 1).owner_mobile = input('Enter mobile phone of dog owner: ', 's');
45
= {dogs.name};
= find(strcmp(names, dog_name));
= 0;
if isempty(index)
message = ['There is no dog in our hotel named ', dog_name];
disp(message);
return;
end
%Computing the bill
d = dogs(index);
bill = 100 * d.days_in_hotel;
if strcmp(d.breed, 'pitbull')
bill = bill * 1.2;
end
if ~strcmp(d.breed, 'None')
bill = bill * 2;
end
%Erasing the dog
dogs(index) = [];
46
47
Running example
>> dogs = dogHotel
Generating a structure for storing dog hotel information
*** Press I for dog check-In, O for dog Check-Out and Q to quit: I
Enter dog name: rufus
Enter dog breed: bulldog
Enter dog age: 1.5
Enter dog special food: None
Enter number of days dog will stay at the hotel: 10
Enter mobile phone of dog owner: 054-777777777
The following dogs are currently staying in the hotel:
Rufus
*** Press I for dog check-In, O for dog Check-Out and Q to quit: I
Enter dog name: king-kong
Enter dog breed: Chihuahua
Enter dog age: 5
Enter dog special food: filet mignon
Enter number of days dog will stay at the hotel: 20
Enter mobile phone of dog owner: 050-88882222
The following dogs are currently staying in the hotel:
rufus
king-kong
48
Running example
*** Press I for dog check-In, O for dog Check-Out and Q to quit: I
Enter dog name: wong
Enter dog breed: pekingese
Enter dog age: 20
Enter dog special food: sushi
Enter number of days dog will stay at the hotel: 1000
Enter mobile phone of dog owner: 050-00000001
The following dogs are currently staying in the hotel:
rufus
king-kong
Wong
*** Press I for dog check-In, O for dog Check-Out and Q to quit: O
Enter the name of the dog you wish to check-out: Lassie
There is no dog in our hotel named Lassie
The bill is 0$
The following dogs are currently staying in the hotel:
rufus
king-kong
Wong
49
Running example
*** Press I for dog check-In, O for dog Check-Out and Q to quit: O
Enter the name of the dog you wish to check-out: king-kong
The bill is 4000$
The following dogs are currently staying in the hotel:
rufus
Wong
*** Press I for dog check-In, O for dog Check-Out and Q to quit: Q
50
Examples
Examples
53
54
55
Read permission
Write permission
%opening target file
fid_out = fopen(f_name_out, 'w');
if (fid_out == -1)
disp('Unable to open target file !');
return;
end
(Continued in the next slide)
56
What is fprintf?
Write data to text
file
Dont forget to close the files after
your done with them
57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
58
59
end
frewind(fid);
%Overwriting original file and writing poem backwards
for i_line = length(poem) : -1 : 1
poem(i_line) or
fprintf(fid, %s\n',
);
poem{i_line} ?
end
fclose(fid);
61
62
63
Name
Badal
Badri
Badrinath
Bahubali
Bahuleya
Bajrang
Balaaditya
Balachan
Balagovind
Balaji
Balakrishna
Balamani
.
Age
77
100
22
95
33
48
78
78
64
16
30
89
Weight Salary
79
86
79
91
77
85
106
86
120
112
76
79
5909
5290
6960
6233
8314
5870
9144
7055
7570
5021
9066
6253
64
85
76
86
116
67
78
113
117
79
60
100
9056
8621
5115
7510
5433
9053
6853
8798
6703
9746
7875 ...
65
'Age'
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
'Weight '
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
'Salary '
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
66