What is the best way to count occurrences of data from an excel file?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Michael Cottingham
am 9 Mär. 2022
Kommentiert: Michael Cottingham
am 30 Mär. 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 Kommentare
Akzeptierte Antwort
Voss
am 9 Mär. 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 Kommentare
Voss
am 23 Mär. 2022
Bearbeitet: Voss
am 23 Mär. 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!