Plot columns against each other depending on user input from imported excel file.

Hi,
I would like to plot the total_cases against days_tracked based on the country that is inputted by the user. The plotting of data is dependent on which country is inputted by the user, however if I for example put Australia as an input it does not plot.
I'm not sure if I have formatted the strcmpi properly as this might be where the problem is. I can't attached the excel file as it exceeds 5MB even as a zip file.
Thanks
covid_data = readtable('owid-covid-data.xlsx');
%Extracting columns from excel
Total_cases = covid_data{:,6};
Days_tracked = covid_data{:,5};
Total_deaths = covid_data{:,9};
New_cases = covid_data{:,7};
New_deaths = covid_data{:,10};
Location = covid_data{:,3};
user_input= input('Please input a valid country','s');
all_countries = covid_data;
if strcmpi(user_input,Location)
plot(Days_Tracked,Total_cases)
else
fprintf('error')
end

1 commentaire

You read the data in as a table; use it instead of creating all new variables.
We don't know what the data are by type; the Location as a cell-string or char string would be ideal candidate to turn into a categorical variable for which can use equality "==" operator. Or, you could make it simpler for the user by giving them a selection from which to choose instead.
Your comparison as written will return an array comparing to the entire vector; you then do nothing to use that to select the subset of data and in MATLAB an IF is True iff all elements of an array are true; hence, unless the data are only those for the chosen country, the test will never match as written.
The whole spreadsheet may be too big, there's certainly nothing preventing saving a representative subset and attaching it --

Connectez-vous pour commenter.

 Réponse acceptée

3 commentaires

Thank you, this link was very helpful.
I implemented the edits into my code however now I get an error saying it doesn't recognise my variable 'total_cases' and an error in total_cases(total_cases == 0) = [].
I dont understand why it doesn't recognise my variable because I've defined it with the correct column of data.
I have attached a sample of the excel data sheet.
covid_data = readtable('owid-covid-data.xlsx');
%Extracting columns from excel
% Total_cases = covid_data{:,6};
% Days_tracked = covid_data{:,5};
% Total_deaths = covid_data{:,9};
% New_cases = covid_data{:,7};
% New_deaths = covid_data{:,10};
user_input= input('Please input a valid country','s');
Location = covid_data(:,3);
CC = table2cell(Location);
CC1 = table2cell(covid_data);
[R C] = size(covid_data);
for i = 1:R
if strcmp((CC(i)),(user_input));
total_cases(i)=cell2mat(CC1(i,6));
days_tracked(i) = cell2mat(CC1(i,5));
end
end
total_cases(total_cases == 0) = [];
days_tracked(days_Tracked == 0) = [];
plot(days_tracked, total_cases)
Try this code, it works,
the userinput had to entered without space.
clearvars
clc
covid_data = readtable('owid-covid-data.csv');
% Extracting columns from excel
% Total_cases = covid_data{:,6};
% Days_tracked = covid_data{:,5};
% Total_deaths = covid_data{:,9};
% New_cases = covid_data{:,7};
% New_deaths = covid_data{:,10};
%totalcases = [];
%daystracked = [];
user_input= input('Please input a valid country: ','s');
Location = covid_data(:,3);
CC = table2cell(Location);
CC1 = table2cell(covid_data);
[R C] = size(covid_data);
for i = 1:R
if strcmp(CC(i),user_input);
totalcases(i)=cell2mat(CC1(i,6));
daystracked(i) = cell2mat(CC1(i,5));
end
end
totalcases(totalcases == NaN) = [];
daystracked(daystracked == NaN) = [];
plot(daystracked, totalcases)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by