Download the lab11
folder, which contains a file named
QuestionsAndAnswers.txt
. Open it to examine its formatting. It contains
quadrubles of lines:
Question, followed by three answers, each one in its own line.
Open App Designer and create the visual layout for your GUI in the Design View, with four GUI components: Try to create something like this:
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:
Create three new properties to store the following information:
zeros(1,2)
startupFcn
callback functionAn 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:
app.UIFigure
in the Component Browser
(or click on the background canvas)
<add StartupFcn callback>
option from the
drop-down menu labeled StartupFcn. A
skeleton for a function named startupFcn
will
appear in the Code View
fopen
, textscan
and fclose
to read the content of your text file
into the property you created in Step 3. For
textscan
, use a format string that is a single
'%s'
and set the Delimiter
property
to '\n'
so that each line is read as a separate
string
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}
As you've done in previous Apps, add a callback function that terminates the program when you push the quit button.
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:
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:
(2) increment the question number
(3a) if the new question number is 1 or 2:
Title
property for the Radio
Button Group to be the text for the new question
Hint: 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
Text
property for each radio
button to be the corresponding answer choice for the new
question
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):
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:
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):