discussed in class In this problem, you will be implementing a gradebook structu
ID: 3601716 • Letter: D
Question
discussed in class In this problem, you will be implementing a gradebook structure, and a custom constructor function to add new entries. Your structure should have three fields, a student name, a student ID number, and a field including an array of grade values for that particular student. 1. You must create a custom constructor function, which will do one of two things based on the number of input arguments: If four arguments are passed to this function (structure name, student name, student ID, and grade array), it should create a new entry in the given structure with the correctly populated fields. For example, if you pass the empty structure called ece72f17 (look up in the struct to see how to make an empty structure) with a new student entry into your function with this example code: ece72f17-gbStructConst (ece72f17,John Doe', 1234, [80 94 79]); then the returned structure should be modified to contain the above student as the only entry, so if I typed ece72f17 (1) . studentID I would get ans1234 That constructor could be used to add students, for example the following code: ece72f17-gbStructConst (ece72f17,'Jane Doe', 4321, [98 92 84]) would add Jane to the gradebook. Secondly, if only three arguments are passed into this function (structure name, student ID, and a grade input), then it should correctly append the grade value into the appropriate student entry's grade array (using the ID to determine the correct student). For example, using the same example from above, if I then input this ece72f17-gbStructConst (ece72f17, 1234, 75); then the returned structure should be modified to add that grade to the student with the matching student ID, so if I typed ece72f17 (1).grades I would get ans [ 80 94 79 75] . If the ID number is not found, it should return an appropriate error message to the user. 1 of 2Explanation / Answer
The following exercise demonstrates programming the block to calculate the mean and standard deviation for a vector of values:
Open the call_stats_block1 model that you saved at the end of Adding a MATLAB Function Block to a Model. Double-click the MATLAB Function block fcn to open it for editing.
A default function signature appears.
Edit the function header line:
function [mean,stdev] = stats(vals)
The function stats calculates a statistical mean and standard deviation for the values in the vector vals. The function header declares vals as an argument to the stats function, with mean and stdev as return values.
Save the model as call_stats_block2.
Complete the connections to the MATLAB Function block as shown.
In the MATLAB Function Block Editor, enter a line space after the function header and add the following code:
% calculates a statistical mean and a standard
% deviation for the values in vals.
len = length(vals);
mean = avg(vals,len);
stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len);
plot(vals,'-+');
function mean = avg(array,size)
mean = sum(array)/size;
More about length
More about len
More about plot
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.