MATLAB Prepare a function m-file containing a function that prints a line (to th
ID: 3598826 • 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.
The help I need is with the third section, I have the function below, and am having a hard time calling it, (and then calling it again), please show me how to properly call this function (also I'm getting an error on the vec2, saying it's unused, is this still okay/correct?):
function[str] = makestructs(vec1,vec2)
str = struct([]);
for i=1:length(vec1)
str(i).name = vec1(i);
str(i).marks = marks(i);
end
end
Explanation / Answer
Solution=============
The third section, wants to you to:
The method you provided makes only one structure at a time, and after correction shall look like:
function[str] = makestructs(nameVector,marksVector)
str = struct([]);
for i=1:size(nameVector)
str(i).name = nameVector(i,:);
str(i).marks = marksVector(i,:);
end
end
Calling this function as:
function mainStr()
marksVector = [[34 55 66 45];[35 33 44 67]];
nameVector = [char('Ron'); char('Jon')];
%Your array of structures
array=makestructs(nameVector,marksVector);
%Calling the function twice
section1Function(array(1));
section1Function(array(2));
end
Let me know if any doubts...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.