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