My app isnt working right

3 ビュー (過去 30 日間)
Kelsey Pettrone
Kelsey Pettrone 2020 年 11 月 10 日
コメント済み: Cris LaPierre 2020 年 11 月 16 日
I want obesity smoking alcohol and drug deaths to all be plotted for a year that the user picks. I tried to do this with the third figure (Axes3) and it isnt working. I have attached the app below along with the data that im working off of. Please help if you know what i'm doing wrong.
data = readtable('RiskFactorAnalysis (1).csv')
userinput = app.CountryDropDown.Value
userinput2 = app.CountryDropDown_2.Value
useryear = app.Slideryear.Value
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
for i = 1:R
if strcmp((CC(i)),(userinput2));
deaths2(i) = cell2mat(CC1(i,4));
end
end
grid(app.Axes,'on')
deaths(deaths == 0) = [];
deaths2(deaths2 == 0) = [];
year(year == 0) = [];
plot(app.Axes,year,deaths,year,deaths2);
xlabel(app.Axes,'year')
ylabel(app.Axes,'deaths per 100000')
legend(app.Axes,userinput,userinput2)
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
grid(app.Axes2,'on')
obesity(obesity == 0) =[];
Drug(Drug == 0) =[];
Alcohol(Alcohol == 0) =[];
smoking(smoking == 0) =[];
bpcombined = [obesity(:), Drug(:), Alcohol(:), smoking(:)]
bar(app.Axes2,year,bpcombined,'grouped');
xlabel(app.Axes2,'year')
ylabel(app.Axes2,'deaths')
title(app.Axes2,userinput)
legend(app.Axes2,'Obesity','Drug','Alcohol','Smoking')
for i = 1:R
if strcmp((CC(i)),(userinput2));
obesity2(i) = cell2mat(CC1(i,5));
Drug2(i) = cell2mat(CC1(i,6));
Alcohol2(i) = cell2mat(CC1(i,7));
smoking2(i) = cell2mat(CC1(i,8));
end
end
grid(app.Axes3,'on')
obesity2(obesity2 == 0) =[];
Drug2(Drug2 == 0) =[];
Alcohol2(Alcohol2 == 0) =[];
smoking2(smoking2 == 0) =[];
bpcombined = [obesity2, Drug2, Alcohol2, smoking2]
bar(app.Axes3,useryear,bpcombined,'grouped');
xlabel(app.Axes3,'year')
ylabel(app.Axes3,'deaths')
title(app.Axes3,userinput2)
legend(app.Axes3, 'Obesity', 'Drug', 'Alcohol', 'Smoking')
  2 件のコメント
Mario Malic
Mario Malic 2020 年 11 月 10 日
What exactly is the problem?
If you put your 'deliverable' function as a helper function in App Designer, program works fine (I see data in UIAxes). Sometimes year isn't correct in the third plot, but that might be due to the slider taking decimal values, and years are integers.
Kelsey Pettrone
Kelsey Pettrone 2020 年 11 月 10 日
Do you know how i could make it so the slider only uses integers?

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

回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 11 月 10 日
編集済み: Cris LaPierre 2020 年 11 月 10 日
+1 on incorporating your function deliverable into your app rather than having it be an external function. You can learn more about how to do that here.
If you only want to plot a year of data in plot 3, you need to tell your code which row of bpcombined to plot. Right now it is plotting all of them.
Also note that you should be careful about plotting the data for userinput2 agains the year from userinput. These are not necessarily the same.
Your code can also be greatly simplified if you keep your data as a table. There is no need to do the cell and cell2mat conversions if you do. You can also eliminate your for loops by taking advantage of logical indexing. Here's what I simplified it to. I made some modifications to your data to allow it to run as a script here (creating figures and axes handels)
data = readtable('RiskFactorAnalysis.csv')
data = 6468x8 table
Entity Code Year Total_Deaths_per_100000 Obesity_Deaths_per_100000 Drug_Deaths_per_100000 Alcohol_Deaths_per_100000 Smoking_Deaths_per_100000 _______________ _______ ____ _______________________ _________________________ ______________________ _________________________ _________________________ {'Afghanistan'} {'AFG'} 1990 204.25 106.32 1.1375 4.5279 92.268 {'Afghanistan'} {'AFG'} 1991 203.23 105.98 1.1606 3.9692 92.117 {'Afghanistan'} {'AFG'} 1992 202.81 106.24 1.1837 3.1644 92.219 {'Afghanistan'} {'AFG'} 1993 204 107.24 1.225 2.5618 92.973 {'Afghanistan'} {'AFG'} 1994 206.43 108.63 1.2769 2.1231 94.398 {'Afghanistan'} {'AFG'} 1995 206.53 108.78 1.3188 1.7129 94.723 {'Afghanistan'} {'AFG'} 1996 207.06 108.94 1.3537 1.4311 95.328 {'Afghanistan'} {'AFG'} 1997 207.83 108.89 1.3942 1.3117 96.243 {'Afghanistan'} {'AFG'} 1998 208.76 108.59 1.4325 1.5724 97.172 {'Afghanistan'} {'AFG'} 1999 209.67 108.57 1.4864 1.4191 98.199 {'Afghanistan'} {'AFG'} 2000 210.86 108.95 1.5436 1.4716 98.89 {'Afghanistan'} {'AFG'} 2001 214.58 111.62 1.5896 1.5082 99.858 {'Afghanistan'} {'AFG'} 2002 218.28 115.6 1.6013 1.5608 99.524 {'Afghanistan'} {'AFG'} 2003 223.12 121.04 1.6075 1.5774 98.897 {'Afghanistan'} {'AFG'} 2004 229.4 127.6 1.6195 1.5957 98.593 {'Afghanistan'} {'AFG'} 2005 234.75 133.59 1.6212 1.5946 97.952
userinput = "Afghanistan"
userinput = "Afghanistan"
userinput2 = "Australia"
userinput2 = "Australia"
useryear = 2003
useryear = 2003
% Extract all info for userinput
ind = strcmp(data.Entity,userinput);
year = data.Year(ind);
deaths = data.Total_Deaths_per_100000(ind);
obesity = data.Obesity_Deaths_per_100000(ind);
Drug = data.Drug_Deaths_per_100000(ind);
Alcohol = data.Alcohol_Deaths_per_100000(ind);
smoking = data.Smoking_Deaths_per_100000(ind);
% Extract all info for userinput2
ind2 = strcmp(data.Entity,userinput2);
year2 = data.Year(ind2);
deaths2 = data.Total_Deaths_per_100000(ind2);
obesity2 = data.Obesity_Deaths_per_100000(ind2);
Drug2 = data.Drug_Deaths_per_100000(ind2);
Alcohol2 = data.Alcohol_Deaths_per_100000(ind2);
smoking2 = data.Smoking_Deaths_per_100000(ind2);
% Create the 3 figures
figure
ax1 = gca;
grid(ax1,'on')
plot(ax1,year,deaths,year2,deaths2); % plot deaths2 against year2, not year
xlabel(ax1,'year')
ylabel(ax1,'deaths per 100000')
legend(ax1,userinput,userinput2)
figure
ax2 = gca;
grid(ax2,'on')
bpcombined = [obesity, Drug, Alcohol, smoking];
bar(ax2,year,bpcombined,'grouped');
xlabel(ax2,'year')
ylabel(ax2,'deaths')
title(ax2,userinput)
legend(ax2,'Obesity','Drug','Alcohol','Smoking')
% Determine which row of bpcombined to display
indY2 = year2==useryear; % again, use year2, not year
figure
ax3 = gca;
grid(ax3,'on')
bpcombined = [obesity2, Drug2, Alcohol2, smoking2];
bar(ax3,useryear,bpcombined(indY2,:),'grouped'); % Note that I am only plotting 1 row of bpcombined here
xlabel(ax3,'year')
ylabel(ax3,'deaths')
title(ax3,userinput2)
legend(ax3, 'Obesity', 'Drug', 'Alcohol', 'Smoking')
  19 件のコメント
Kelsey Pettrone
Kelsey Pettrone 2020 年 11 月 16 日
編集済み: Cris LaPierre 2020 年 11 月 16 日
legend(app.Axes2,(userinput +' '+['Obesity','Drug','Alcohol','Smoking']),...
(userinput2,+' '['obesity','drug','alcohol','smoking']))
i tried it its not working
Cris LaPierre
Cris LaPierre 2020 年 11 月 16 日
That's not the same as what I shared. You must use strings (double quotes), not chars (single quotes) for this syntax to work.

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

カテゴリ

Help Center および File ExchangeBiotech and Pharmaceutical についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by