Starting from:
$35

$29

Homework 5 Solution

In this homework, we will go through a few advanced data structures in MATLAB, such as cell arrays, classes, chars, as well as dataset importing in MATLAB. You will be importing in the sheriris dataset and encapsulating it in classes. After obtaining all the data, you will be required to write some methods for the class you just created. Also note that some operations will require you to do some research, but not too much!

    1. Load in the sheriris dataset with the command load sheriris. You should obtain a 150x4 matrix called meas, as well as a 150x1 cell array called species in your workspace. This is a very popular dataset, and you can quickly google sheriris to see what the columns of meas represent.

Create a class called Flower in its own .m le. In your Flower class, you should have the following properties { petalWidth (double), petalLength(double), sepalWidth (double), sepalLength (double) and species (char). The ith species corresponds to the ith row of information in meas. Note that you do not need to specify the data type of the properties when you are declaring a class the following properties are just here to impress on you what type of properties you would be expecting.

Create a constructor for your class. It should take in 4 doubles and a char array corre-sponding, in order, to the ve properties of your class.

Now, import the entries from meas and species into MATLAB and store them in a 150x1 cell array of Flower instances. You can either use a for loop to import the entries, or use a more elegant way to make all entries in one line (this would require some research on object formation in MATLAB). Either way is ok. However, note that the name of the species are stored as cell arrays; make sure to extract from the cell as a char and remove trailing white spaces before storing them in the Flower instances. There is a single function that will do this, which you should be able to nd with a quick google search.

Create a method called getSWidth for the Flower class, which will return the sepal length of the object. Use this on the 30th Flower in your cell array, and check (using ==) that this is the correct value by looking that the corresponding entry in meas.

Create another method called report for the Flower object, which will print out a state-ment on the command window and report the details about the Flower object. This function does not need to return anything. It should print out a statement of the fol-lowing form if the ower is 5.1 cm in sepal length, 3.5 cm in sepal width, 1.4 cm in petal length and 0.2 cm in petalWidth, and the species is setosa, for example: \The length and width of its sepal are 5.1 cm and 3.5cm respectively, while the length and width of its petal are 1.4cm and 0.2cm respectively. It belongs to the setosa species."












1

More products