What is the best way to count occurrences of data from an excel file?
9 views (last 30 days)
Show older comments
Michael Cottingham
on 9 Mar 2022
Commented: Michael Cottingham
on 30 Mar 2022
I am trying to count the occurences of strings in an excel doc but there are some complications.
My dataset is large but a sample would be the following: (there is no pattern)
C01 C02 " "
B01 C02 C02 D04
C05 C01 " "
... (there are many rows and columns)
The " symbol occurs because in a previous step the data was converted from delimited to non-delimited, there are also some 'Error" values in the data.
I want my output to tell me: (using example data above)
C01 occurred 2
C02 occurred 3
etc.
I have looked into using tabulate(), hist(), and groupcounts() but i haven't figured it out.
What method would you recommend?
0 Comments
Accepted Answer
Voss
on 9 Mar 2022
There are different ways to read an excel doc, and the function you use might treat some things differently (in particular, the cells with ").
Here are two functions you might use for reading the data, readcell() and xlsread(). The counting of the data in the cells is done the same way in both cases, in the function report_occurrences(), defined at the bottom.
C = readcell('data.xlsx')
report_occurrences(C);
[~,~,C] = xlsread('data.xlsx')
report_occurrences(C);
function report_occurrences(C)
[uC,~,jj] = unique(C(:));
counts = zeros(numel(uC),1);
for ii = 1:numel(uC)
counts(ii) = nnz(jj == ii);
end
result = [uC num2cell(counts)].';
sprintf('%s occurred %d\n',result{:})
end
6 Comments
Voss
on 23 Mar 2022
Edited: Voss
on 23 Mar 2022
C = readcell('example_data.csv','delimiter',' ')
C = C(~cellfun(@(x)isa(x,'missing'),C)) % remove the 'missing's from cell C
report_occurrences(C); % now run the function like usual
function report_occurrences(C) % function definition is the same as before
[uC,~,jj] = unique(C(:));
counts = zeros(numel(uC),1);
for ii = 1:numel(uC)
counts(ii) = nnz(jj == ii);
end
result = [uC num2cell(counts)].';
sprintf('%s occurred %d\n',result{:})
end
More Answers (0)
See Also
Categories
Find more on Spreadsheets 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!