LanguageMatlab Part A Extend a simple user interface Create
Language:Matlab
Part A:
 Extend a simple user interface Create a new m-file called Assign5PartA_.m and enter the following MATLAB code fragment:
 while 1
 shapeList = {\'Circle\',\'Square\'};
[selection,ok] = listdlg(\'PromptString\',\'Select the next shape:\',...
\'SelectionMode\',\'single\',...
\'OKString\',\'Enter\',...
\'CancelString\',\'No more\',...
\'ListString\',shapeList);
if ~ok break; end
 prompt={\'Enter shape dimension 1\',\'Enter shape dimension 2\'};
 title = \'Shape dimension dialog box\';
 numlines = 1; defaults = {\'0\',\'0\'};
 options.Resize = \'on\';
 options.WindowStyle = \'normal\';
 options.Interpreter = \'none\';
 inputvalues=inputdlg(prompt,title,numlines,defaults,options);
 end
 The code fragment implements a simple graphical user interface (GUI) that queries the user to select shapes from a menu, and then allows the user to enter two numerical shape dimensions for each shape. Note that the number of shape dimensions is fixed at two, regardless of the number of dimensions that are actually required. A circle only requires a diameter dimension; the second dimension is not required. A square also only requires one dimension that gives the common length of all sides of the square. Note also that the code fragment already uses cell arrays. In your submitted code, identify all of the cell arrays and structures that are used, and briefly explain in comments what they are used for. Consult the on-line documentation for the function listdlg() and then modify the given code fragment to extend the capabilities of the user interface in the following ways.
1) Increase the number of object shapes to include ellipses, triangles and rectangles by making suitable modifications to the initialization of the cell array shapeList. The two dimensions of an ellipse are to be major and minor diameters. The two dimensions of a triangle are to be the length of a base side, and the corresponding height to the third corner of the triangle. The dimensions of a rectangle are to be the height and width.
2) Provide an additional menu that allows the use to enter a shape colour. The available shape colours should be ‘red’, ‘yellow’, ‘blue’, ‘green’, ‘orange’ and ‘violet’. The two buttons at the bottom of the colour select menu should be labelled “Enter” and “No colour”.
3) Provide a loop counter shapecounter that keeps track of the number of shape instances that have been entered by the user.
4) When the user selects the button labelled “No more”, enhance the program so that it prints out the message “The number of entered objects was xxx” where “xxx” is replaced by the integer count of the number of entered objects.
Part B:
Shape database and area calculation The user interface that you developed in Part A is now to be modified to store the entered shape information into a single structure array called shape. The n’th shape you enter should be the structure shape(n). The structure array should have sub-fields of ‘ID’, ‘color’, ‘dimensions’ and any other fields you wish. Your code should calculate the areas of the shapes you enter and print a summary table of results, an example of which is shown below.
The number of entered objects was 2
No. ID Color Area
1 Circle red 3.141593
2 Square yellow 1.000000
Enter your solution in a new file called: Assign5PartB_.m . Your code should use at least one sub-function that accepts as a single argument the structure array shape.
Solution
 function [] = Assign5PartB_1255986()
%clear the command window, the variables from the workspace and close all
 %open figure windows
 clear; clc; close all;
%initialization of the indexing variable and the shape structure array
 numShapes = 0;
 shape = {};
while 1
     %iterate the indexing variable
     numShapes = numShapes + 1;
   
     %create a GUI of shapes for the user to select from
     shapeList = {\'Circle\',\'Square\', \'Ellipse\', \'Triangle\', \'Rectangle\'};
     % selection holds the index of the selected shape
     [selection,ok] = listdlg(\'PromptString\',\'Select the next shape:\',...
         \'SelectionMode\',\'single\',...
         \'OKString\',\'Enter\',...
         \'CancelString\',\'No more\',...
         \'ListString\',shapeList);
   
     % ok is 1 if a shape was selected; otherwise, ok is 0
     if ~ok
         numShapes = numShapes -1;
         break;
     end
   
     shape(numShapes).objectShape = shapeList{selection};
   
   
     %create a GUI of colours for the user to select from
     colourList = {\'Red\',\'Yellow\', \'Blue\', \'Green\', \'Orange\', \'Violet\'};
     % selection holds the index of the selected colour
     [colour_selection,ok1] = listdlg(\'PromptString\',\'Select the shape color:\',...
         \'SelectionMode\',\'single\',...
         \'OKString\',\'Enter\',...
         \'CancelString\',\'No colour\',...
         \'ListString\',colourList);
   
     % ok1 is 1 if a colour was selected; otherwise, ok1 is 0
     if ~ok1
         numShapes = numShapes -1;
         break;
     end
   
     shape(numShapes).colour = colourList{colour_selection};
   
   
     %create a GUI for the user to enter two dimensions for the selected
     %shape
     prompt={\'Enter shape dimension 1\',\'Enter shape dimension 2\'};
     title = \'Shape dimension dialog box\';
     numlines = 1;
     defaults = {\'0\',\'0\'};
     options.Resize = \'on\';
     options.WindowStyle = \'normal\';
     options.Interpreter = \'none\';
     inputvalues = inputdlg(prompt,title,numlines,defaults,options);
   
     % str2num(inputvalues{1}) is the value of the first dimension
     % str2num(inputvalues{2}) is the value of the second dimension
     shape(numShapes).dimensions = [str2num(inputvalues{1}) str2num(inputvalues{2})];
   
 end
%call to the area_calculation function to calculate area for each of the
 %selected objects in the shape structure array
 area = area_calculation(shape);
%print out the total number of shape objects the user selected
 fprintf(\'The number of entered objects was %d\ \', numShapes);
%print the label for the columns
 fprintf(\'No.\\tID\\t\\tColor\\t\\tArea\ \');
 for i = 1:numShapes
     shape(i).area = area(i);
     fprintf(\'%d\\t%s\\t\\t%s\\t\\t%.6f\ \', i, shape(i).objectShape,...
         shape(i).colour, shape(i).area);
 end
 end
function [area] = area_calculation(shape)
 % calculate the area of shape objects in a given structure array and return
 % the results in an area array
%use the shape structure array size information to initialize the area
 %array
 [row, number_of_shapes] = size(shape);
 area = zeros(row, number_of_shapes);
for i = 1:number_of_shapes
     switch shape(i).objectShape
         case \'Circle\'
             %area = 4*pi*radius^2
             area(i) = 4 * pi * ((shape(i).dimensions(1))/2)^2;
         case \'Square\'
             %area = sidelength^2
             area(i) = (shape(i).dimensions(1))^2;
         case \'Ellipse\'
             %area = pi * major diamemter * minor diameter
             area(i) = pi * (shape(i).dimensions(1)) * (shape(i).dimensions(2));
         case \'Triangle\'
             %area = 0.5 * base * perpendicular
             area(i) = 0.5 * (shape(i).dimensions(1)) * (shape(i).dimensions(2));
         case \'Rectangle\'
             %area = length * breadth
             area(i) = (shape(i).dimensions(1)) * (shape(i).dimensions(2));
     end
 end
 end



