MATLAB: How to seperate these number from a cell and put it into a matrix.

covid19data from excel to matlab

I have tried systax "cell2mat" but it didnt work.

Best Answer

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.