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

Prepare a function m-file containing a function that prints a line (to the comma

ID: 3598059 • Letter: P

Question

Prepare a function m-file containing a function that prints a line (to the command window), where the line consists of a string, the value of a (scalar) unsigned integer, and the values of the elements in a vector. All these components should print on one line with white space between the different components. 2. The input will be a cell array. The first cell will contain the string. The second cell will contain the (scalar) unsigned integer having class uint8. The third cell will contain the vector of class double. This function should work if the vector is of length 1,2, 3, or 4. There will be no output variables. In the second section of your script m-file, generate 2 different cell arrays to test your function. You can use any values you want. However, the length of the vector should be different for the 2 different cell arrays. (This will demonstrate that your function works for more than just one particular vector length.) 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. 3. 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

Given below are answers to 2 and 3 in 1st picture. Please post rest of the questions, 1 question per post.

Answer 2:

save the following code a file name printStrScalarVector.m

%a function receives a cell array as input
% the 1st eleemtn in cell array is a string, the 2nd is an int and the
%3rd element is a vector

%the function prints all in a single line
function printStrScalarVector(arr)
%arr{1} is the 1st cell array element, since it is a string we use %s
fprintf(" %s", arr{1});
%use %d to print integer
fprintf(" %d", arr{2});
v = arr{3};
%iterate over the vector
for i = 1:length(v)
%print double using %f
fprintf(" %f", v(i));
end
  
end

You can test the fucntion using

%test the function
%define 2 vectors
v1 = [2.5 , 4.25];
v2 = [5.1, 10.2, 15.3, 20.4];
%create 2 cell arrays using the vectors
arr1 = { 'hello', 111, v1};
arr2 = { 'good morning', 222, v2};
%use the function to print
printStrScalarVector(arr1);
printStrScalarVector(arr2);

output

Answer 3

save the code in file named printStructure.m

function printStructure(s)
fprintf(" %s", s.name);
for i = 1 : length(s.marks)
fprintf(" %f", s.marks(i));
end
end

To test the function you can use

student(1).name = 'john';
student(1).marks = [ 90.5, 80];
student(2).name = 'bob';
student(2).marks = [ 78.0, 69.0, 90.5, 80.0];
printStructure(student(1));
printStructure(student(2));

output