# Load COVID-19 case data from John Hopkins database
Loading, processing and plotting the data from the John Hopkins COVID-19 database. The data is automatically read from the online repository, thus, you need a internet connection. The data can be found here: https://github.com/CSSEGISandData/COVID-19.
# How to (see runAll.m):
type = 'confirmed'; % 'confirmed','deaths','recovered'
[dataMatrix] = readCoronaData(type);
[dataTable,timeVector,mergedData] = processCoronaData(dataMatrix);
plotCoronaData(timeVector,mergedData,{'Denmark','US','Germany','China'},type);
Axel Ahrens (2021). Load COVID-19 case data from John Hopkins database (https://github.com/aahr/covid-19_data_analysis), GitHub. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
Wonderful and practical code.
I would add two lines to the end of runAll.m to get time series data as a row vector for a given country (US for example)
=============================
idx = find(strcmp(mergedData, "US"));
time_series=mergedData{idx, 2}(1,:)
============================
Regards
@safiul mollick : I tried with a couple of recent MATLAB version but not as late as 2016. Do you have access to a newer version? If not, can you contact me via email and I try to fix the problem.
I am new here, I am using Matlab 2016
I got this error too
Undefined function or variable 'newline'.
Error in readCoronaData (line 87)
if m == 1 && contains(dataMatrix{m,n},newline)
Error in runAll (line 4)
[dataMatrix] = readCoronaData(type);
I updated to the latest version of MATLAB. Get this error now;
Error using cell2table (line 69)
The VariableNames property is a cell array of character vectors. To assign multiple
variable names, specify nonempty names in a string array or a cell array of character
vectors.
Error in processCoronaData (line 21)
dataTable = cell2table(dataMatrix(2:end,:),'VariableNames',varNames);
@Alex: What MATLAB version do you use? Is the error fixed with the newest version of the code?
Thanks Axel!
I got this error, any ideas?
Undefined function or variable 'newline'.
Error in readCoronaData (line 83)
if m == 1 && contains(dataMatrix{m,n},newline)
Thank you very much Axel for the code, very interesting
Used it the first time today with commit 18ef41f & Matlab 2019a (9.6, update 5)
I did encounter the following issues:
1. handle of strings & cell arrays in processCoronaData
Error using strrep
Cell elements must be character vectors.
Error in processCoronaData (line 17)
varNames = strrep(varNames,'/','_');
Error in runAll (line 7)
[dataTable,timeVector,mergedData] = processCoronaData(dataMatrix);
--> fixed it by usage of
function str = strrep2(str, old, new)
str = cellfun(@(str) rep(str, old, new), str, 'UniformOutput', false);
function str = rep(str, old, new)
if ischar(str)
str = strrep(str, old, new);
end
https://www.gomatlab.de/strrep-cell-elements-must-be-character-arrays-t44174.html
2. data in dataset of today in processCoronaData
25: for m = 1:size(dataMatrix,1)
26: for n = 1:size(dataMatrix,2)
fix for me -> skip last row 'malawi' entry and the last column in the datamatrix
for m = 1:size(dataMatrix,1)-1
for n = 1:size(dataMatrix,2)-1
3. issue with the time format when assigning to the Timevector
38: timeVector(n-4) = dataMatrix{1,n};
fix for me :
dataMatrix{m,n} = datetime(dataMatrix{m,n},'InputFormat','MM/dd/yy');
Cheers
Hartwig
Starting from today, I get the following error:
Error using cell2table (line 69)
The VariableNames property is a cell array of character vectors. To assign multiple variable names, specify names in a
string array or a cell array of character vectors.
Error in processCoronaData (line 311)
dataTable = cell2table(dataMatrix(2:end,:),'VariableNames',varNames);
The issue is that the data matirx is not read properly. Changing
if contains(lineBreakData(lineBreakIdx+2:end-1),'"') %there is a comma within state/province
to
if contains(lineBreakData(lineBreakIdx:end-1),'"') %there is a comma within state/province
in the read function fixes the issue for me. However, I don't know if there are any side effects.
I think the issue is coming from your version of Matlab and, particularly, the function cell2table.
"Starting in R2019b, you can specify table variable names that are not valid MATLAB® identifiers. Such variable names can include spaces, non-ASCII characters, and can have any character as the leading character. When you access such a variable name, enclose it quotation marks.", from: https://www.mathworks.com/help/matlab/ref/table.html
@Christopher: This seems to be an issue only in Mac OS. But it should be fixed now.
@Axel
I'm running on Mac OSX Catalina 10.15.4. Just tried R2019a. Same error, but with error message as Holger reported March 25th.
@Christopher: Thats odd. I will add a fix in 10 mins.
Hi Axel
I currently use R2018B.
It seems to me that it is the backslash that causes problems when I run. Anyway I can fix it myself for my version of Matlab.
Hi Christopher, for me everything runs smooth with the current GitHub version. What MATLAB version do you use?
Hi Axel
Downloaded today 2020-03-30
Problems still present in processCoronaData
>> runAll
Error using cell2table (line 57)
'Province/State' is not a valid variable name.
Error in processCoronaData (line 16)
dataTable = cell2table(dataMatrix(2:end,:),'VariableNames',varNames);
Error in runAll (line 6)
[dataTable,timeVector,mergedData] = processCoronaData(dataMatrix);
@Holger: Yes, indeed. They changed again. Its fixed now. I am checking the code at least once per day to keep it running.
Hi Axel, I fetched your latest version - but the problem still remains. I´m not sure if there have been further changes in data format by JHU... Many thanks for your efforts!
Hi Holger. Try the most recent version. There was a change in the John Hopkins repository data format. Please get in touch if it still does not work.
Error using cell2table (line 58)
'Province/State' is not a valid table variable name. See the documentation for isvarname or matlab.lang.makeValidName for more information.
Error in processCoronaData (line 16)
dataTable = cell2table(dataMatrix(2:end,:),'VariableNames',varNames);
Error in runAll (line 6)
[dataTable,timeVector,mergedData] = processCoronaData(dataMatrix);
Thank you for this set of functions. These allow customization for those of us playing with Covid-19 tracking and prediction.