CISC106-010,
Fall 2007
Answers to
the PracticeQuestions_2007_11_7
Cell
Arrays, Content indexing vs. Cell indexing
Repetition
Structures (while loops, for loops, nested loops) and Selection Structures
(If/Else)
SPLITing
a string using regexp built-in function
Script
M-files, Function M-files, Test scripts
A1. >> name(3)
A2. >> fullName = [firstName ' ' lastName]
A1.
>> fprintf('The
answer is:%9.2f\n', 12.34567)
A2.
>> fprintf('My
name is %s and I am %d years old.\n', name, age)
A1. (b) 1x3 matrix of doubles
A2. (a) 1x1 cell array
A3. (a) 1x3 cell array
A4. i (i.e.,
the 2nd char of the string mike)
A5. mike
A6. mycell{3} returns the matrix in the 3rd coloum of the cell array mycell. That is,
mike
john
A7. Any of the
expressions below would work.
>>
mycell{3}(2,1)
>>
mycell{1,3}(2,1)
>>
mycell{1,3}(2)
A8. >> mycell{1,3}(1, :) = 'adam'
v =
[10 20 30 40];
index
= 1;
while
(index < length(v))
fprintf('%d ', v(index));
index = index + 1;
end
% while
fprintf('\n');
for
i = [1:6]
fprintf('%d ', i);
end
% for
fprintf('\n');
function
result = howManyMs(str)
% inputs:
% str: the string that we are searching the
m's
% outputs:
% result: number of m's in the string
% Example:
%
>> howManyMs('how let the dogs out?')
%
%
ans =
% 0
%
%
>> howManyMs('I like M&M')
%
% ans =
% 0
%
%
>> howManyMs('I like m&m')
%
%
ans =
% 2
result
= 0;
index
= 1;
while
(index <= length(str))
if (str(index) == 'm')
result = result +1;
end % if
index = index+1;
end
%while
return;
end
% function
function
result = howManyMs_for(str)
% inputs:
% str: the string that we are searching the
m's
% outputs:
% result: number of m's in the string
% Example:
%
>> howManyMs('how let the dogs out?')
%
%
ans =
% 0
%
%
>> howManyMs('I like M&M')
%
% ans =
% 0
%
%
>> howManyMs('I like m&m')
%
%
ans =
% 2
result
= 0;
for
index = [1: length(str)]
if (str(index) == 'm')
result = result +1;
end % if
index = index+1;
end
% for
return;
end
% function
A5. The transpose of the matrix v is printed to the screen. That is, the output on the screen will be:
10
40 70
20
50 80
30
60 90
%
open both files, one file to read the other one to write
fromFile
= fopen('input.txt', 'r');
toFile = fopen('output.txt', 'w');
%
read one line from the input file
str
= fgets(fromFile);
%
if the input file is empty or there is no line left to read, fgets returns
%
the EOF (i.e., -1).
while
(str ~= -1)
% write the line into the output file
fprintf(toFile, '%s', str);
% read the next line from the input file
str = fgets(fromFile);
end
% while
%
since you are done with the files, now close both files
fclose(fromFile);
fclose(toFile);
A1. Note that, after spliting the given string into sub-strings (using the given delimiter), regexp returns all the sub-strings in a cell array. That is why the expression
>>
regexp('my team is: galatasaray',
':' , 'split')
returns (b) 1x2 cell array.
>>
regexp(mystr, ',' , 'split')
The expresion above would return the answer below:
>>
regexp(mystr, ',' , 'split')
ans
=
'eagles'
' 70000' ' 5 ' ' 1'
A1. Any of the Unix commands below would work
>
chmod g+w copyFromFile.m
>
chmod g=w copyFromFile.m
>
chmod 620 copyFromFile.m
A2. Gives read
and write permissions to the file copyFromFile.m