Question
(MATLAB) %hw6q1.m nameEntered = false; studentStruct = []; % variable to repeat the whole process if some more student to enter repeatOver = true; countStudent
(MATLAB) %hw6q1.m nameEntered = false; studentStruct = []; % variable to repeat the whole process if some more student to enter repeatOver = true; countStudent = 1; % vectors to store the values in each iteration name_array = []; age_array = []; rating_array = []; % main loop while(repeatOver) % inputting name from user name = inputdlg('Enter your name', 'Input name', [1.5 30], {''}); while nameEntered == false if isempty(name) name = inputdlg('You can''t live this field empty.Please re-enter your name', ... 'Input name', [1.5 30], {''}); else nameEntered = true; end end % adding name to name_array name_array{countStudent} = {name}; ageEntered = false; % asking the 2nd question age = inputdlg('Enter your age', 'Input age', [1.5 30], {''}); while ageEntered == false if isempty(age) age = inputdlg('You can''t live this field empty.Please re-enter your age', ... 'Input age', [1.5 30], {''}); else ageEntered = true; end end age = str2num(age{1}); % adding age to age_array age_array{countStudent} = age; % checking the age for validity if age < 10 uiwait(warndlg('You must be at least 10 years old.')); hw6q1 elseif age > 120 msg = ['If youre really over 120 years old, more power to you', ... ' but I dont quite believe you.']; uiwait(warndlg(msg)); hw6q1 else % Asking the 3rd question quest = 'How much do you like this class?'; title = 'Question'; btn1 = 'Bad'; btn2 = 'Good'; btn3 = 'Best'; class_rating = questdlg(quest,title,btn1,btn2,btn3, 'Good'); % adding rating to rating_array rating_array{countStudent} = {class_rating}; quest_a = 'Whether there is another student to input data?'; title_a = 'More Student ?'; btn_a = 'Yes'; btn_b = 'No'; response = questdlg(quest_a,title_a,btn_a,btn_b, 'No'); if string(response) == "Yes" countStudent = countStudent + 1; else % creating the structure and adding the arrays to % the structure studentStruct.Name = name_array; studentStruct.Age = age_array; studentStruct.ClassRating = rating_array; % generating the filename to store it as .mat file fname = [num2str(countStudent), 'studentsData']; % saving the structure save(fname, 'studentStruct'); repeatOver = false; nameEntered = false; end end end
Run hw6q1.m on your own, and enter in data for 5 fictional students. Then create a script called hw6q2.m that loads the 5studentsData.mat file you created, and then: a) Counts how many students gave each of the 3 class rating options and saves it as a 1x3 vector called classRatings. In other words, if 1 student gave the lowest rating (L), 1 student gave the middle rating (K), and 3 gave the highest rating (J), then the vector should end up with the values [1 1 3]. b) Plots the three values of classRatings as a barplot with the lowest rating (1) on the left and highest rating (3) on the right. c) Sets the title of the bar plot to be Click on a bar below to find the mean age of those students. d) Waits for the user to click on one of the bars (use ginput). e) Uses the round function to figure out which rating the user clicked closest to (think about whether the x or y value will help with thatmaybe try it out yourself first). f) Then computes the mean age for the students who gave the chosen rating, and updates the title to be The average age of students rating X was Y years old. Where X is replaced with the text of that rating choice and Y is replaced with the mean age. Hint: num2str may help you with turning the number Y into a character array. g) Parts a-f should be able to correctly run no matter what the data are (i.e., do not hardcode any responses) or which bar the user clicks on. In order to do this, the script should handle the special case where no students are in the bar graph area the user clicks on (users can be silly!)in this case, the title should be updated to Please click on a rating with nonzero responses. and then parts d-f should execute again.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started