Changing function file to get the k-th element of 1st row of a .txt file and adding another parameter?

1 vue (au cours des 30 derniers jours)
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 commentaires
jonas
jonas le 28 Mai 2018
Modifié(e) : jonas le 28 Mai 2018
What is AddCandy()? Also, you are overwriting candy in every loop (intentional?).
Perhaps include a description of your desired output.
Emanuele Joy
Emanuele Joy le 28 Mai 2018
Oops, I forgot to include that. AddCandy should produce an output something like:
CandyName: {'Rock candy' 'Gum' 'Lollipop' 'Taffy' 'Hershey Kisses'}
CandyCost: [35 5 10 3 18]
CandyAmount: [0 0 0 0 0]
CandyGoodness: [4 5 5 2 7]
numCandyTypes: 5
My original code produced all this except for the new row CandyGoodness, which is what I'm trying to add with candyData(2,k). I'm just trying to produce an output listing the parameters of the candy name, the cost, the amount, and the goodness, which is what AddCandy does. Does that clear things up?

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Low-Level File I/O dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by