Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

imageimage

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

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. We ll call it from a script in problem 3. Deliverables: 1. Modified function file for GetCandy 2. Modified function file for AddCandy a. Read in a second row of numeric data (the goodness) b. Pass the values from the second row into AddCandy 3. The modified data file with the goodness row and at least one new column 4. Show calling your GetCandy file with your new data file a. Should take in another variable - candyGoodness b. Should add in another field in the struct to store that value Step by Step Instructions: First check that you can read in the existing data file and it works o candy = GetCandy( CandyData.txt ) Edit the CandyData.txt by adding a candy type of your own Check that it still works Add in a row to the CandyData.txt with goodness values Check that it still works o Note: It will work because the original script is just going to ignore those extra values First modify GetCandy so that it extracts the second row of data as well as the first O Put a breakpoint after the csvread in GetCandy. Note that this is a 2xn array - it s read the data, you just need to get it out o Modify the for loop to loop over the number of columns in candyCosts Note: length works because (at least for right now) the number of columns is the bigger of the two dimensions. But size is the right command here O Change the call to AddCandy to get out the kth element of the first row Now modify AddCandy so that it takes in another parameter (candy goodness) o Pretty much just copy and paste what was done for candyCost, then change the name(s) Go back and modify GetCandy so that it passes the kth element of the second row (the goodness) as well as the cost Self-check: Use your own values here, but it should look like: >> candy GetCandyModified ( CandyDataModified.txt ) candy = struct with fields: CandyName: { Rock candy CandyCost: [35 5 10 3 18] CandyAmount: [0 0 0 0 0] CandyGoodness: [6 1 1 7 8] numCandyTypes: 5 Gum Lollipop Taffy Smarties } This my CandyDataModified.txt Rock candy, Gum, Lollipop, Taffy, Smarties 35, 5, 10, 3, 18 6, 1, 1, 7, 8

Step by Step Solution

3.51 Rating (154 Votes )

There are 3 Steps involved in it

Step: 1

It seems like you have provided the code for a MATLAB program that deals with managing candy d... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

An Introduction to Programming with C++

Authors: Diane Zak

8th edition

978-1285860114

More Books

Students also viewed these Electrical Engineering questions

Question

What elements of multimedia-based instruction facilitate learning?

Answered: 1 week ago