Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ENGR 131: Elementary Computer Programming

Lab Exercise #7

INSTRUCTIONS

Complete the exercises below and upload them Canvas as a single MATLAB script file using the naming convention “ENGR131_23S_Lab##_abc123.zip”, replacing abc123 with your Case ID, and ## with the two-digit lab number.

For example, if Dr. Williams were submitting Lab 6 it would be ENGR131_23S_Lab07_mrw8.zip

For your script, please perform the following:

1. You may use the code and notes from class, the textbook, lecture videos, MATLAB’s documentation, and anything you find using Google to solve these problems.

2. Use comments as appropriate to indicate your thoughts and how your code works (or is supposed to work). This is 5 points (10%) of your grade.

QUESTIONS

Microwaves are common machines in many households. The one we will be modeling in this lab is like those found in many convenience stores in that it has a limited number of time options available. Our model will also collect data for post-cooking analysis.

1. CREATE THE GUI (11 PTS, 1 PT EACH)

Create a two-panel GUI similar to that shown in Figure 1 by dragging and placing items from the Component Library in the Design View of App Designer (left side of environment). Give each item a meaningful name in the Property Inspector (right side of environment). It doesn’t have to be exactly as shown, but it must include the following elements:

A. A discrete knob for selecting the cook timel. You will have to edit the values to have Off, Low, Med, and High time options.

B. Buttons for running the washer and plotting. These can be any color you choose.

C. A switch to represent the door.

D. An axis area to represent the “glass” of the door.

E. An axis to plot the recorded data.

F. A gauge to show the time remaining. It can be any type you choose.

G. A slider to indicate the total run time.

H. Switches for pre-setting the appropriate time to cook Pizza or a Burrito.

I. A lamp for user feedback

J. A radio button group to select which type of data to show, changing the names to Pizza, Burrito, and Other as shown in Figure 1.

K. A drop down menu with options for Blue, Red, Green, and Black.

Fig 1. An example of the interface for a microwave

2.CREATE APP PROPERTIES (2 PTS, 1 PT EACH)

To share data between callbacks, in the Code View, create app Properties in the Code Browser (left side of environment) using the “+” to add to properties to your app. Give these appropriate names similar to the following:

A. TotalCookTime

B. FoodCooked

3. CREATE USER DEFINED FUNCTIONS (8 PTS, 4 PTS EACH)

For proper functionality, you will need the following user defined functions:

A. A startupFcn.

a. With the Code Browser set to Callbacks, press the “+” to bring up the Add Callback Function window. Select startupFcn from the Callback pulldown menu

b. Initialize the door switch value to ‘Open’

c. Set the color of the status lamp to red

d. Set the TotalCookTime property to 0.

B. A function to set the plotting line and color type

a. Select Functions in the Code Browser. Pres the “+” to create a function template in your app code. Change the name and add an input argument to pass in the selected color selection. Be sure to keep the (app) input as well.

b. Use a selection statement to set the results variable to an appropriate line and color specification (ex. r-- for a red dashed line) based on the color selected by the user.

4. CREATE CALLBACKS (25 PTS)

Now it’s time to set up all the controls and displays for our GUI. For each element below, in the Design View, right click on element and select “Callback -> Add callback”. The specific name may be different for each element, but the offered suggestion is the one you want. For most controls, the default callback starts with assigning the value of the control to a default variable. You will use this variable in your callback

A. Door switch (2 pts)

a. Use a selection statement to change the color of the status lamp to red and the plotting axes representing the glass to whie when open and both to black when closed. Remember to use the contains command when checking the values of strings

B. Pizza Switch (2 pts)

a. Use a selection statement to set the value of the cook time knob to Low when on or Off when the switch is turned off.

C. Burrito Switch (2 pts)

a. Use a selection statement to set the value of the cook time knob to Med when on or Off when the switch is turned off.

D. Run Button (14 pts)

a. Use a while loop to wait until the door (switch) is closed. Being sure to include a 0.5 s pause.

b. Use a while loop to wait until the cook time knob is not Off, being sure to include a 0.5 pause.

c. Change the status lamp to yellow.

d. Use selection statements to determine if pizza or a burrito is selected.

i. Record the type of food cooked by creating a vector with either a 1 in the first column (for pizza), the second column (for burrito) or the third column (for neither pizza or burrito being selected).

ii. Concatenate this vector onto the end of the FoodCooked property. Ex. after seven runs, the FoodCooked property will be a 7x3 vector.

e. Use selection statements to get the cook time from the Cook Time knob.

i. If it is set to Low, set the cook time variable to 2 seconds (it’s a REALLY powerful microwave).

ii. If it is set to Med, the cook time is 5 s.

iii. If it is set to High, the cook time is 10 s.

f. Use a while loop to simulate the cooking time base on the cook time set in D.e. In this while loop, do the following:

i. Compute the time remaining and if < 0, set to 0 (we don’t want to deal with negative time remaining)

ii. Set the value of the time remaining gauge to the result from f.i.

iii. Turn the color of the plotting axis representing the glass red.

1. If you want to be fancy, have the glass start out red, but fade to black over the course of the cook time.

iv. Use the drawnow command.

v. Include a 0.1 s pause.

g. Reset the system:

i. Change the status lamp to green.

ii. Change the cook time knob to Off.

iii. Reset the pizza and burrito switches to Off.

h. Update the total cook time.

i. Add the cook time to the total cook time property.

ii. Set the slider to display the total cook time.

E. Data Plotting (4 pts)

a. Get the color and line specification by calling the user defined function 3.B and pass in app and the value from the pull-down menu.

b. Use a conditional statement to plot either the pizzas, burritos, or Other types of food cooked over the run based on the value of the radio buttons (they will be logical 1, if that button is selected, 0 if not selected).

i. For the plotting, you will need an independent variable that goes from 1 to the number of rows in the FoodCooked property

ii. Remember that the data for each food type is recorded in each column of the FoodCooked property.

c. Plot the selected data using the color spec from part E.a.

d. Change the Titles and axis labels accordingly to the data presented.

Hints:

• As you code this, proceed incrementally. Get one part working at a time to reduce frustration and improve your understanding of the syntax.

• You may find it helpful to include outputs to the Command window as you get things working. Using something like disp(‘cooking) in the run button code at the start of the while loop to indicate what is going on can be helpful. Do not leave these in the final submission, they are just for you.