CS 99

Summer 2002: Lab 7                                                                                                   07.25

Fun with functions

1.    Objectives

Completing all tasks in this assignment will help you practice with functions in MATLAB

First skim, then carefully read the entire assignment before starting any tasks.

2.    I need a perm’, part 2
Modify the permutation function from yesterday’s lecture so that it permutes not the elements from [1, n] (where n was the old input parameter, a number), but the elements of its new, replacement parameter x (a vector). 

Your function should return an error if the input argument has fewer than 2 elements (you may find the MATLAB functions
length and isempty useful).

Call your function
xperm.m. Include an appropriate set of help comments.

3.    A super-duper, all-purpose integer reader
You are going to write a function called readIntPrompt that behaves like MATLAB’s own input function but differs from it in two ways:

1.     It will only accept integer values.
2.     And will not quit prompting the user until  the user enters an acceptable integer, only then
         will it return the value entered by the user to the Workspace. 


Your function will of course use
input in the body of its code, but it will test to see if the value entered by the user is acceptable.

The
value entered by the user must not be


Your function should take a single string parameter as input (call it
prompt), and return a single double parameter (call it value).  If the user does not supply a string for prompt, your function should return an error.

If the user enters a value that is not an integer, your program should display That value is not an integer! then prompt the user again using the same string the user supplied the first time.




Sample output:

>>z = readIntPrompt(Enter an integer: ');

Enter an integer:

That value is not an integer!

Enter an integer: yo

??? Undefined function or variable 'yo'.

 

Enter an integer: 'hi'

That value is not an integer!

Enter an integer: 3 +  2*i

That value is not an integer!

Enter an integer: pi

That value is not an integer!

Enter an integer: 1/0

 

Warning: Divide by zero.

> In Papillon:MATLAB:readintprompt.m at line 22

That value is not an integer!

Enter an integer: 2*realmax
That value is not an integer!

Enter an integer: 6
>>


Your function will have the following structure

function value = readIntPrompt( prompt )

% HEADER comment

% ensure prompt is a string, give error otherwise

% call MATLAB’s input function using parameter prompt and assign the result to value

% display error message and repeat call to function input until value entered is acceptable

When you finish, bang away at the keyboard and see if you can make the function crash.

 

4.    Submitting Your Work
Type your name (and your partner’s name if you have one), student ID, and the date at the top of each M-file.  Print each file and sign them along with your partner.  Give the signed documents to the teaching assistant at the end of the lab session.