ALL DONE INMATLAB------------------------------ CandyData.txt: Rock candy, Gum, Lollipop, Taffy32, 15, 10, 3 AddCandy.m: function [ candyList ]
Question:
ALL DONE INMATLAB------------------------------
CandyData.txt:
Rock candy, Gum, Lollipop, Taffy32, 15, 10, 3
AddCandy.m:
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 theoutput% should be the same because you're adding to the struct
%% LAB TIP: You will end up adding another input parametercalled candyGoodness to this function. % To make this work you will also need to update whathappens 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 thefield% isfield asks if there is a field called CandyCostin the struct
%% This branch will execute if the structure candyList isdefined. % 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 acandyGoodness value to an % existing field called CandyGoodness using the []operator. %% This branch will execute if the structure candyList is not yetdefined. % New fields will be created and edited.else candyList.CandyName = cell(1,1); %Create a field named CandyName candyList.CandyName{1} = strCandyName; % Add a nameto the field named CandyName candyList.CandyCost = candyCost; % Create a fieldnamed CandyName candyList.CandyAmount = 0; %% LAB TIP: Be sure to create the CandyGoodness fieldand assign a value to it here. end
% Just count the amount of names so that you can save the numberof candies in the structure. candyList.numCandyTypes = length( candyList.CandyName );
end
GetCandy.m:
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 thisis a cell% array), amount, and cost.% LAB TIP: Later you will modify the code towork with another array of% goodness values (a numeric rating from1-10). candy = struct;
%% Read in the headers and the amount of candies from afile% file has candy name as headers; the second row is thecosts% csvread(name, rowstart, columnstart)fid = fopen( filename, 'r' );% Get all the data as a cell arraystrCandies = textscan(fid, '%s', 'Delimiter',',');strCandies = strCandies{1};fclose(fid);
%% Try adding a break point here so that you can see how thedata is stored. % LAB TIP: Doing this will help you locate the candyGoodnessvalues.candyData = csvread( filename, 1, 0 );
% Use this function to add in each candy type - this functionmakes sure% that all of the parts of the struct are set up correctlyfor k = 1:length( candyData ) % LAB TIP: Notice that we are just indexing data fromthe first row right now. % Once AddCandy() gets modified it will need to takea new parameter from % the second row in candyData. candy = AddCandy( candy, strCandies{k},candyData(1,k));end
end