Changing function file to get the k-th element of 1st row of a .txt file and adding another parameter?
1 view (last 30 days)
Show older comments
I have a function that reads in a .txt file of candy names, cost, amount, and the total number of candy types. What I want to do is to add another parameter of "candy goodness" to it. This is what I have so far: (Note: if I switch the for loop the read 1:length(candyData), it reads everything but the row I'm trying to add.)
function [ candy ] = GetCandy( filename )
candy = struct;
fid = fopen( filename, 'r' );
strCandies = textscan(fid, '%s', 'Delimiter',',');
strCandies = strCandies{1};
fclose(fid);
candyData = csvread( filename, 1, 0 );
for k = 1:size( candyData )
candy = AddCandy( candy, strCandies{k}, candyData(1,k), candyData(2,k));
end
end
What I'm trying to do is to change AddCandy so that it gets the k-th element of the first row (which is why I changed it to size() instead of length()) and have it read the last row of my .txt file, but it's telling I have too many input arguments in my AddCandy line.
This is the .txt file I'm trying to read.
Rock candy, Gum, Lollipop, Taffy, Hershey Kisses
35, 5, 10, 3, 4
4, 5, 5, 2, 7
The last row (4, 5, 5, 2, 7) is the candy goodness rating I'm trying to read through the script.
What do I need to change here? I thought candyData(2,k) would read the last row but it's not working.
2 Comments
Answers (0)
See Also
Categories
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!