MATLAB Answers

Plot multiple conditions in same figure using for and if loop.

6 views (last 30 days)
Cate may
Cate may on 26 Oct 2020
Commented: Cate may on 26 Oct 2020
Hi,
I would like to plot the total cases against days tracked for the 6 countries mentioned in key_locations. The code currently only works and plots for a single country because strcmp can only compare two strings, but I would like to compare 6 strings (one for each country) to the cell data and plot the graphs all on the same figure. This is what I've written so far and a sample of the excel sheet is attached.
Thank you
covid_data = readtable('owid-covid-data.xlsx');
key_locations = ["Australia" "China" "India" "Indonesia" "Malaysia" "Vietnam"];
Location = covid_data(:,3);
CC = table2cell(Location); %converts table to cell array
CC1 = table2cell(covid_data);
[R C] = size(covid_data);
for i = 1:R
if strcmp(CC(i),key_locations(1));
totalcases(i)=cell2mat(CC1(i,6));
daystracked(i) = cell2mat(CC1(i,5));
newcases(i) = cell2mat(CC1(i,7));
end
end
totalcases(totalcases == NaN) = []; %NaN is currently undefined
daystracked(daystracked == NaN) = [];
newcases(newcases == NaN) = [];
figure(1)

  0 Comments

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 26 Oct 2020
Edited: Cris LaPierre on 26 Oct 2020
You might as well keep your data in a table. Coverting it to a cell array doesn't help you.
Creating your plot can be done quite simply using the ismember function and gscatter.
covid_data = readtable('owid-covid-data.xlsx');
key_locations = ["Australia" "China" "India" "Indonesia" "Malaysia" "Vietnam"];
% Find reports for key_locations
Location = ismember(covid_data.location,key_locations);
% Plot data for key_locations. Group by country.
gscatter(covid_data.days_tracked(Location),covid_data.total_cases(Location),covid_data.location(Location))

  4 Comments

Show 1 older comment
Cate may
Cate may on 26 Oct 2020
Hello, I've tried the code and it's giving me the shape that I want however I want to use the plot function instead of gscatter. When I substitute gscatter for plot I get an error saying unrecognised variable days_tracked. How can I fix this?
Thanks
Cris LaPierre
Cris LaPierre on 26 Oct 2020
The plot function does not have a grouping option. If you just replaced gscatter with plot, you should get an 'Invalid data argument' error. Otherwise, be sure to reference the table variables the same way I have - tableName.variableName.
There are a couple ways to do this. I would try using findgroups and splitapply, but you could also use a for loop if you want.
covid_data = readtable('owid-covid-data.xlsx');
key_locations = ["Australia" "China" "India" "Indonesia" "Malaysia" "Vietnam"];
% Find reports for key_locations
Location = ismember(covid_data.location,key_locations);
% Identify groups in the data
G = findgroups(covid_data.location(Location));
% Plot each group
figure
hold on
splitapply(@plot,covid_data.days_tracked(Location),covid_data.total_cases(Location),G);
hold off
legend(key_locations)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!