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

Write a Matlab function called newstruct.m which takes a cell array of field nam

ID: 3827182 • Letter: W

Question

Write a Matlab function called newstruct.m which takes a cell array of field names and a cell array of contents and creates a struct variable with the given field names and then assigns the contents to each field in the order in which they appear in the cell array. Assume that the arrays of names and contents are the same length. Program specifications and testing are discussed below. I have included examples of the use of this function in the .zip file containing the programming checkpoint. You can type load example newstruct or load example2newstruct at the command line to load either of the two examples and see the two input cell arrays and corresponding output struct. There is a built-in Matlab function called cel12struct that accomplishes this goal, but you may not use it. If str='jack', then mystruct. (str) = 15 dynamically assigns the new field jack to mystruct and populates it with the value 15.

Explanation / Answer

For implement function start with the empty struct array with fields:-

Output:-

S =

This works also for non-empty structs:

Output:-

S=

a:0

b: 0

<<struct_name>>.(<<field_name_str>>) = <<value>>. Also, the if statement tests whether a condition holds or not, so no need to test if it is true. Finally, namelist should be stored as a cell array.

dynamic field reference where you put a string in the parenthesis as seen on the line defining stuff:-

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote