Lab 11: Create a survey
Step 0: Getting started
Create a folder named survey
on your Desktop and set
the Current Folder in MATLAB to your survey
folder.
Step 1: Create a text file with questions & answers
In the MATLAB editor, start a New Script and enter text for two survey questions with three answers for each question (you can make up your own text, but keep it short). Enter the questions and answers on separate lines, for example:
How do you like your tea? with a touch of milk and sugar steeped loose Jasmine tea leaves iced cold with sugar How do you like your eggs cooked? an omelette with cheese and ham in creamed eggs on toast wispy eggs boiled in a broth
(Don't worry about the initial purple font color — the text
will change to black when you save the file.) When saving the file for
the first time, select All Files (*.*) from the drop-down
menu labeled Format:, be sure to save the file in your
survey
folder, and enter a full file name with
.txt
for the extension, for example:
Step 2: Create your GUI layout in App Designer
Open App Designer and create the visual layout for your GUI in the Design View, with four GUI components:
- a Label at the top
- a Radio Button Group for the Q&A
- a Text Area to display an initial introduction and (later) results of the survey
- two Buttons, to control the flow of the survey and quit the program when done
When saving your App for the first time, be sure to save it in
your survey
folder.
You're welcome to arrange the components as you'd like, for example:
Step 3: Create new properties to store survey information
Create three new properties to store the following information:
- the current question number, initialized to 0
- the Q&A text from your text file, intialized to {} (an empty cell array)
- a vector to store the user's answers (encoded as 1,2, or 3),
initialized to
zeros(1,2)
Step 4: Create a startupFcn
callback function
An App has a startup function that you can define, which is executed automatically when the App starts up. For this program, you'll create a startup function to read the contents of your text file into the property you just created to store this information. To do this, follow these steps:
- select
app.UIFigure
in the Component Browser (or click on the background canvas) - under the Callbacks tab, select the
<add StartupFcn callback>
option from the drop-down menu labeled StartupFcn. A skeleton for a function namedstartupFcn
will appear in the Code View - add code that uses
fopen
,textscan
andfclose
to read the content of your text file into the property you created in Step 3. Fortextscan
, use a format string that is a single'%s'
and set theDelimiter
property to'\n'
so that each line is read as a separate string - the
textscan
function will return a cell array of strings nested inside another cell array, which you can assign directly to the property, e.g.app.questions = textscan( ... )
. Reassign the property to the inner cell array, e.g.app.questions = app.questions{1}
Step 5: Create a callback function for the quit button
As you've done in previous Apps, add a callback function that terminates the program when you push the quit button.
Step 6: Add a callback function to control the flow of the survey
A single button, such as the one labeled start survey in the above GUI (we'll refer to this as the "survey button"), together with the current question number, can be used to control the flow of the survey. In Step 3 above, a new property was created to store the current question number. The question number was initialized to 0, indicating that the survey has not yet begun. As the user progresses through the survey, the question number can be incremented to 1, then to 2, and finally to 3, signifying the end of the survey and time to display the results. The progression through these steps will be controlled by the user pushing the survey button — each time the user pushes this button, the question number will be increased and appropriate actions performed.
Add a ButtonPushedFcn
callback function for the
survey button. The main tasks to be performed by this callback function are:
- record the user's answer to the current survey question (once the survey begins)
- update the question number for the survey
- if there are still more questions to ask, then update the GUI display with the next question and answer options; otherwise show the results of the survey on the GUI
Note that when the survey button is pushed, the question number will have the value 0, 1, or 2.
In more detail, this callback function should perform the following actions:
(1) if the current question number is 1 or 2:
- store a 1, 2, or 3 in the corresponding location of the vector of user answers, depending on which of the three radio buttons is selected
(2) increment the question number
(3a) if the new question number is 1 or 2:
- update the
Title
property for the Radio Button Group to be the text for the new questionHint: the question number can be used to determine the index of this string in the cell array of strings created from the text file in Step 4
- update the
Text
property for each radio button to be the corresponding answer choice for the new question - update the
Text
property for the survey button — if the new question number is 1, change this text to "next question", otherwise change it to "survey results"
(3b) otherwise (i.e., the new question number is 3):
- set the
Value
property for the Text Area component to a cell array of strings that capture multiple lines of text in response to the user's answers to the survey questions.Hint: here's one way that this response could be constructed:
- create a cell array of three different response strings to use for the three answer choices for the first question
- create a cell array of three different response strings to use for the three answer choices for the second question
- use the user's answer to each question (i.e. the 1, 2, or 3 stored in the locations of the vector of answers) as an index to select the corresponding response string
- put the two response strings together in a cell array
The following snapshots show one progression through the demonstration program, after the user pushes the start survey button (we're not sure why MATLAB sometimes displays a horizontal gap in the radio button text):