Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
PSYCH 20B
Homework 3 Write a clear, thoroughly commented Matlab program that collects survey data: Put opening comments that describe what the program does (collects and saves data for a short survey) and gives your name and the date (or just the year). After the opening comments, the program should be divided into 4 major sections: “SETUP”, “ENTER SUBJECT NUMBER”, “SURVEY”, and “SAVE & EXIT”. Use clear, prominent headings for these sections. The SETUP section should include all the typical setup (setting screen preferences, hiding the mouse cursor, defining xmid and ymid, etc.). You can seed the random number generator if you want, but it’s not necessary for this assignment. Set the font (use “Arial”) and the text-size (make it 1/30th the screen height, and don’t forget to round). Define any necessary key-numbers (don’t forget to use 'UnifyKeyNames' first). And do any other setup you need, including computing coordinates for text that will be displayed. Make the screen background white. Define mainTextColor as black (using either an RGB triplet or a single value), and define warningTextColor as an RGB triplet for orange. The ENTER SUBJECT NUMBER section should get echoed input using the following prompt: Enter subject number: The prompt text should be vertically and horizontally centered on the screen. HINT: You can’t use 'center' as an input to the GetEchoString function, so you’ll need to use the width and height of the prompt text to compute the coordinates. If the entry is not a whole number between 1 and 1000, clear the screen and display “INVALID SUBJECT NUMBER” for 1 second, in the middle of the screen, using warningTextColor; then prompt the experimenter to enter the subject number again. Repeat this process as necessary until a “valid” subject number is entered. Then clear the screen and continue. The SURVEY section should present the following sequence of survey questions, using mainTextColor except where otherwise specified. • Display the following text, vertically and horizontally centered on the screen: Press the spacebar to begin the survey Wait for the spacebar to be pressed before continuing. No other key-presses should have any effect. • Clear the screen, and display the following text: Are you a native English speaker? (Y) Yes (N) No The text should be vertically centered on the screen, with a blank line after “Are you a native English speaker?”. For the horizontal position, use 'centerblock' which centers the text box as a whole, but makes each line start at the same horizontal position; that’s often preferable for multiple choice items such as this, because it makes the options more readable. • Record the subject’s native-speaker status as 0 if “n” is pressed or as 1 if “y” is pressed. Only pressing “n” or “y” should result in continuing to the next survey item; no other key should produce any effect. The subject should not have to press
after their response.
• Clear the screen, and display the following text:
Are you registered to vote?
(Y) Yes
(N) No
The text should be vertically centered on the screen, with a blank line after “Are you registered to
vote?”. For the horizontal position, use 'centerblock'
• Record the subject’s voter registration status as 0 if “n” is pressed or as 1 if “y” is pressed. Only
pressing “n” or “y” should result in continuing to the next survey item; no other key should produce any
effect. The subject should not have to press after their response. IMPORTANT: Only a fresh
key-press should register; make sure the “n” or “y” already being down from the previous question isn’t
immediately registered as the response for this question also (causing this question to appear to get
“skipped”).
• Clear the screen, and get echoed input using the following prompt, with a space after the colon:
Please enter your age in years:
The prompt text should be vertically and horizontally centered on the screen. If the entry is not a number
between 10 and 120 (inclusive), clear the screen and display “INVALID AGE” for 1 second, in the
middle of the screen, using warningTextColor; then prompt the subject to enter their age again.
Repeat this process as necessary until a “valid” age is entered. Then continue to the next survey item. Do
not require the age to be an integer. NOTE: Be sure to test your code to make sure that the subject can’t
crash your program by entering a weird input.
• Clear the screen, and get echoed input using the following prompt (with a space after the colon):
Please enter your favorite color:
The prompt text should be vertically and horizontally centered on the screen. If the input is fewer than 3
characters long, clear the screen and display “INVALID COLOR” for 1 second, in the middle of the
screen, using warningTextColor; then prompt the subject to enter their favorite color again. Repeat
this process as necessary until a “valid” color is entered.
• Clear the screen, and display the following text:
The survey is complete.
Thank you!
The text should be vertically and horizontally centered on the screen, with a blank line between “The
survey is complete” and “Thank you.”
The SAVE & EXIT section should do the following:
• Save the subject ID number and all four survey responses in a .mat data file. The filename should start
with psych20bhw3_subj followed by the entered subject number. For example, if the entered
subject number was 5, then the filename should be psych20bhw3_subj5.mat. You don’t have to
submit that data file with your homework.
HINT: You can save multiple objects to a data file using the save function. The first input is the
filename in quotes (you don’t need to include the .mat extension). The rest of the inputs are the names
of the objects you want to save (in quotes). For example, if you had a variable called
heightInInches and a variable called weightInLbs and you wanted to save them both in a file
called heightAndWeightData.mat, you would use the following command:
save('heightAndWeightData', 'heightInInches', 'weightInLbs')
• Wait for the “e”, “x”, “i”, and “t” keys—and no other keys—to all be held down at once. This allows the
experimenter to exit the program after the subject leaves. The purpose of requiring a key combination to
exit is that you don’t want the subject to be able to exit to the Matlab window accidentally by pressing a
key. Unless all four keys are down while no other key is down, the program should remain on the thank-
you screen. For instance, if you hold down some fifth key (such as spacebar) while pressing the “e”, “x”,
“i”, and “t” keys, that should not exit.