Question: Problem 1 Add a candy goodness row to the CandyData.txt file. This is a number between 1 and 10 that says how much you like
Problem 1
Add a candy goodness row to the CandyData.txt file. This is a number between 1 and 10 that says how much you like that candy. Also add in a few more of your favorite candy types.
Alter GetCandy and AddCandy so that you can read and store the new data.
For this problem, as in the last lab, check your solution by calling it from the command line. Well call it from a script in problem 3.
Everything I have to start for GetCandy function:
function [ candy ] = GetCandy( filename )
%GetCandy Read in and set up candy from file % Get headers, costs
% Going to store our candy in a struct % We will be adding one array for candy name (note that this is a cell % array), amount, and cost. % LAB TIP: Later you will modify the code to work with another array of % goodness values (a numeric rating from 1-10). candy = struct;
%% Read in the headers and the amount of candies from a file % file has candy name as headers; the second row is the costs % csvread(name, rowstart, columnstart) fid = fopen( filename, 'r' ); % Get all the data as a cell array strCandies = textscan(fid, '%s', 'Delimiter',','); strCandies = strCandies{1}; fclose(fid);
%% Try adding a break point here so that you can see how the data is stored. % LAB TIP: Doing this will help you locate the candyGoodness values. candyData = csvread( filename, 1, 0 );
% Use this function to add in each candy type - this function makes sure % that all of the parts of the struct are set up correctly for k = 1:length( candyData ) % LAB TIP: Notice that we are just indexing data from the first row right now. % Once AddCandy() gets modified it will need to take a new parameter from % the second row in candyData. candy = AddCandy( candy, strCandies{k}, candyData(1,k)); end
end
Everything I have to start for the AddCandy function:
function [ candyList ] = AddCandy( candyList, strCandyName, candyCost) %AddCandy Add a candy type to the candy struct % Make sure to add in one element to each array % This is one of the few times when the input and the output % should be the same because you're adding to the struct
%% LAB TIP: You will end up adding another input parameter called candyGoodness to this function. % To make this work you will also need to update what happens in the if % else statements below
% First check to see if we have a list yet in the struct % If so, just add it in, otherwise need to make the field % isfield asks if there is a field called CandyCost in the struct
%% This branch will execute if the structure candyList is defined. % Values will be added to each field using the array [] operator. if isfield( candyList, 'CandyCost' ) candyList.CandyName = [ candyList.CandyName, strCandyName ]; candyList.CandyCost = [ candyList.CandyCost, candyCost ]; candyList.CandyAmount = [ candyList.CandyAmount, 0 ]; % none, yet %% LAB TIP: Add another line here that will add a candyGoodness value to an % existing field called CandyGoodness using the [] operator. %% This branch will execute if the structure candyList is not yet defined. % New fields will be created and edited. else candyList.CandyName = cell(1,1); % Create a field named CandyName candyList.CandyName{1} = strCandyName; % Add a name to the field named CandyName candyList.CandyCost = candyCost; % Create a field named CandyName candyList.CandyAmount = 0; %% LAB TIP: Be sure to create the CandyGoodness field and assign a value to it here. end
% Just count the amount of names so that you can save the number of candies in the structure. candyList.numCandyTypes = length( candyList.CandyName );
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
