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

2 vues (au cours des 30 derniers jours)
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
dpb
dpb le 24 Oct 2020
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

VBBV
VBBV le 24 Oct 2020
  3 commentaires
VBBV
VBBV le 25 Oct 2020
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

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by