Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB help, need a function- not a script- that does the following: Created wit

ID: 2085401 • Letter: M

Question

MATLAB help, need a function- not a script- that does the following:

Created with your code. Your MATLAB code must be a function (not a script) and it will have only one input argument, which is the name of the data file as a string. The function will not prompt the user for any additional information. Specifically, the MATLAB function must perform the following sequence of operations: Read a comma-separated-values text file into a matrix variable with the filename given by the input argument. Extract the five columns of the matrix as variables x1, y1, y2, y3, and z1. Convert x1, which has units of second, to have units of ms. Rescale and offset each of the y1, y2, and y3 variables with a known linear equation. Refer to Table 1 for the constants of the linear equations and meaning of these variables. Find the index that the z1 variable first crosses a known threshold of 2.5 volts. Offset the entire x1 variable so that it is equal to zero at that index found in Step 4. Extra Credit: Offset each of y1, y2, and y3 with the mean of each of those variables over a range of indices where x1 is negative. Create a single plot of x1 versus y1, y2, and y3 with meaningful axis labels for the x-axis and y- axis, including units. Provide a legend that clearly indicates which line is y1, which is y2, and which is y3. Extra Credit: In the function, programmatically set the size of the graphic to be 3 inches by 3 inches, line widths of 1 point, label and legend font size of 9 points, and the entire plot is in greyscales. Save the plot as a PDF file.

Explanation / Answer

my_script.dat

5, 2, 4, 4, 3
3, 4, 6, 9, 9
3, 7, 2, 8, 1
4, 2, 7, 9, 2

my_script.m

function my_script(filename)
%part 1
fileID = fopen(filename);
C = textscan(fileID,'%d %d %d %d %d','Delimiter',',');
fclose(fileID);
%part 2
x1 = cell2mat(C(:,1));
y1 = cell2mat(C(:,2));
y2 = cell2mat(C(:,3));
y3 = cell2mat(C(:,4));
z1 = cell2mat(C(:,5));%converts cell to array
%part 3
x1 = x1*1000;%to convert Seconds into milliseconds multiply it with 1000
%part 4
y1 = 2*y1-1;
y2 = 3*y2+2;
y3 = 0.75*y3-3;
end