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

2 次查看(过去 30 天)
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 个评论
dpb
dpb 2020-10-24
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 --

请先登录,再进行评论。

采纳的回答

VBBV
VBBV 2020-10-24
  3 个评论
VBBV
VBBV 2020-10-25
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by