What is the best way to count occurrences of data from an excel file?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Michael Cottingham
el 9 de Mzo. de 2022
Comentada: Michael Cottingham
el 30 de Mzo. de 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 comentarios
Respuesta aceptada
Voss
el 9 de Mzo. de 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 comentarios
Voss
el 23 de Mzo. de 2022
Editada: Voss
el 23 de Mzo. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!