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

MATLAB Prepare a function m-file containing a function that prints a line (to th

ID: 3596120 • Letter: M

Question

MATLAB

Prepare a function m-file containing a function that prints a line (to the command window), where the line consists of a string and the values of the elements in a vector. All these components should print on one line with white space between the different components.

The input will be a structure. The first field will contain the string. The second field will contain the vector of length 4 and of class double. There will be no output variables.

In the third section of your script m-file, generate a structure array having 2 component structures. You can use any values you want. You will call your function twice. The first time, you will use the first component structure as input. The second time, you will use the second component structure as input.

Explanation / Answer

function x = printStruct(a,ind)
%print name
fprintf("%s ",a(ind).name);
%for loop to print scores
for x=a(ind).scores
fprintf("%.1f ",x);
end
%print newline char
fprintf(" ");
end
%define structure
a = struct('name',{'Abe','Brett'},'scores',{0:3,1:4});
%print first value
printStruct(a,1);
%print second value
printStruct(a,2);


% sample output
%Abe 0.0 1.0 2.0 3.0
%Brett 1.0 2.0 3.0 4.0