Need Help plotting things from a table

I have attached the table i am working from. I want the user to input a country and then i want to plot the deaths for every year in that country.
I though strcmp might work but it isnt. this is all I have so far.
clear all
clc
data = readtable('RiskFactorAnalysis (1).csv');
userinput = input('Enter a Country: ','s');
Country = data(:,1);

 採用された回答

VBBV
VBBV 2020 年 10 月 22 日
編集済み: VBBV 2020 年 10 月 22 日

1 投票

Try this
clearvars
clc
data = readtable('RiskFactorAnalysis (1).csv');
userinput = input('Enter a Country: ','s');
Country = data(:,1);
CC = table2cell(Country)
CC1 = table2cell(data)
[R C] = size(data);
for i = 1:R
if strcmp((CC(i)),(userinput));
deaths(i) = cell2mat(CC1(i,4));
year(i) = cell2mat(CC1(i,3));
end
end
deaths(deaths == 0) = [];
year(year == 0) = [];
plot(year,deaths)
xlabel('year')
ylabel('deaths')

3 件のコメント

Kelsey Pettrone
Kelsey Pettrone 2020 年 10 月 22 日
That worked! thank you so much!
I'm Trying to do a bar graph of all the types of deaths now but it isnt working. I thought it would work the same way but it isnt. the error code is saying
"Error using bar (line 171)
X must be same length as Y.
Error in try1 (line 40)
bar(year,bpcombined,'grouped');"
Here is my code. I would be so greatful if you could help me again.
for i = 1:R
if strcmp((CC(i)),(userinput));
obesity(i) = cell2mat(CC1(i,5));
Drug(i) = cell2mat(CC1(i,6));
Alcohol(i) = cell2mat(CC1(i,7));
smoking(i) = cell2mat(CC1(i,8));
end
end
figure
hold on
grid on
obesity(obesity == 0) =[];
Drug(Drug == 0) =[];
Alcohol(Alcohol == 0) =[];
smoking(smoking == 0) =[];
bpcombined = [obesity, Drug, Alcohol, smoking]
bar(year,bpcombined,'grouped');
xlabel('year')
ylabel('deaths')
VBBV
VBBV 2020 年 10 月 23 日
bpcombined = [obesity; Drug; Alcohol; smoking];
yearall = [year.' year.' year.' year.'];
bar(yearall,bpcombined.')
xlabel('year')
legend('Obesity','Drug','Alcohol','Smoking')
Rik
Rik 2020 年 10 月 23 日
I suspect the variable names are tripping the spam filter. I had to remove both of the prior comments from the spam filter.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

質問済み:

2020 年 10 月 21 日

コメント済み:

Rik
2020 年 10 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by