summing random numerical excel data in matlab?

1 view (last 30 days)
help pls. how about if i have this kind of excel file in which contains 2 columns (name and vaue) randomly arranged. what i want to output is the summarizing or addition of same name. how can i do this
name1 10
name2 20
name3 30
name4 40
name5 50
name5 20
name4 20
name3 10
name1 90
name2 10

Answers (1)

Nir Rattner
Nir Rattner on 5 Aug 2014
You can use the “xlsread” function to read in the data from your Excel spreadsheet. Once the data is imported, you can use standard MATLAB operations.
For your next step of summing the data and grouped by the name, you can make use of the “unique” and “accumarray” functions. The “unique” function will index your string names. You can sum the values according to their indices using the “accumarray” function.
Here is some example code:
[num, txt, raw] = xlsread('Book1.xlsx');
[keys, ~, subs] = unique(txt);
results = [keys num2cell(accumarray(subs, num))]

Community Treasure Hunt

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

Start Hunting!