Cell arrays and structure | Complete Solution
Practice Exercise 2
The second function you will need to write will provide convenient access to the number of a given element that exists within an amino acid.
Write a function that returns the atom count, given an amino acid structure and the name of an element. Use the function header:
function result = atomcount(amino, atomname)
where amino is a structure variable containing information about a single amino acid and atomname is a string containing the name of an element.
In your function, you will need to query the atomname variable, comparing it to the 5 element names to find out which it is. Once you have found a match, you can retrieve the relevant variable from the amino variable’s structure. Hint: Matlab’s switch control structure is useful in this situation.
Practice Exercise 3
Your next task is to write a function that retrieves a single amino acid structure from the structure array, given its name. Use the function header:
function result = findamino(s, aminoname)
where s is a structure array of amino acids and aminoname is a string containing the name of the amino acid to find in the structure. Your function should give an error message (by calling the error() function) if the given amino acid name could not be found in the structure array.
Important: When writing and testing your function, you can use the structure array s that you have generated using yourgeneratearrays() function, that contains 4 amino acids. However, the function findamino() needs to work correctly when given, as input, a structure array s of any size, containing any list of amino acids, not necessarily including the 4 shown in exercise 1. When your function is assessed, a variety of amino acid structure arrays will be used. For this reason you will not be able to use theswitch control structure, you will need to loop through each item in the structure array in order to compare the amino acid names.
Practice Exercise 4
The final function will calculate the weight of an amino acid, given the amino acid structure array, the element weight cell array and the name of the amino acid to calculate the weight of. Use the function header:
function result = aminoweight(s, w, aminoname)
where s is the the amino acid structure array, w is element weight cell array and aminoname is a string that contains the name of the amino acid.
Because you have already written the functions findamino() and atomcount(), this function should be relatively straightforward. Using these two functions, you can retreive a single structure from array s, retreive the atom count for each element and then multiply these by the relevant weights in w to calculate the total molecular weight of the amino acid.
Important: Do not hardcode the element weights in your calculations. You must retrieve them from w. When your function is assessed, different weights values will be used in the cell array w. You can assume that the names of the elements will be the same (Oxygen, Carbon, Nitrogen, Sulfur, Hydrogen)