How can I seperate these number from a cell and put it into a matrix.

1 visualización (últimos 30 días)
I have tried systax "cell2mat" but it didnt work.
  2 comentarios
Star Strider
Star Strider el 14 de Feb. de 2020
You will have to provide (attach) that file if you want help with it. I was able to find it online although not able to download it.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 14 de Feb. de 2020
Try this:
[num,txt,raw] = xlsread('2019_nC0v_20200121_20200126_cleaned.xlsx');
vars = regexp(txt(1,:), ',', 'split');
varsc = string(vars{:})
newcell = regexp(txt(2:end), ',', 'split');
C1 = cellfun(@(x)str2double(x(:,1)), newcell);
C2 = cellfun(@(x)x(:,2), newcell);
C3 = cellfun(@(x)x(:,3), newcell);
C4 = datetime(cellfun(@(x)x(:,4), newcell), 'InputFormat','MM/dd/yyyy');
for k = 1:4
C(:,k) = cellfun(@(x)str2double(x(:,k+4)), newcell);
end
COVID19 = table(C1,C2,C3,C4,C(:,1),C(:,2),C(:,3),C(:,4));
COVID19.Properties.VariableNames = {'Nr','Province/State','Country','Date Last Updated','Confirmed','Suspected','Reecovered','Deaths'};
and:
Check = COVID19(1:5,:)
produces:
Check =
Nr Province/State Country Date Last Updated Confirmed Suspected Reecovered Deaths
__ ______________ __________________ _________________ _________ _________ __________ ______
0 {'Shanghai'} {'Mainland China'} 21-Jan-2020 9 10 0 0
1 {'Yunnan' } {'Mainland China'} 21-Jan-2020 1 0 0 0
2 {'Beijing' } {'Mainland China'} 21-Jan-2020 10 0 0 0
3 {'Taiwan' } {'Taiwan' } 21-Jan-2020 1 0 0 0
4 {'Jilin' } {'Mainland China'} 21-Jan-2020 0 1 0 0
I cannot get the variable names to import correctly or parse correctly. I added them manually.
  5 comentarios
Hai Luu
Hai Luu el 15 de Feb. de 2020
Cool. Any possible way to classify the people come from Main Land, Taiwan,.... for instance.?
Star Strider
Star Strider el 15 de Feb. de 2020
I am not certain what you mean by ‘classify’. If you want to assign numbers to them, use the findgroups function.
For example —
CountryID = findgroups(COVID19(:,3));
You can do the same for 'Province/State' by referring to column 2 instead.
To combine them, use either of these:
CountryStateID = findgroups(COVID19(:,[2 3]));
CountryStateID = findgroups(COVID19(:,2), COVID19(:,3));
Both produce the same result.
There are other options as well. See: Grouping and Binning Data in the Preprocessing Data documentation section.

Iniciar sesión para comentar.

Más respuestas (1)

Jon
Jon el 14 de Feb. de 2020
You will not be able to put your data from the cell array you show to a matrix because you have both numeric and non-numeric data in your cell array and a MATLAB matrix can be either numeric or non-numeric but not both.
I suggest instead that you store your data in a table where it will be very natural to work with it
  2 comentarios
Hai Luu
Hai Luu el 15 de Feb. de 2020
the reason I want to put it into a matrix is because I want to do something with machine learning :D. I have tried put numbers like this file but with systax cell2mat, I still cannot put it into a matrix.
Star Strider
Star Strider el 15 de Feb. de 2020
@Hai Luu — Please see my Answer!

Iniciar sesión para comentar.

Categorías

Más información sobre Biological and Health Sciences 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!

Translated by