My app isnt working right
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Mario Malic
am 10 Nov. 2020
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.
Antworten (1)
Cris LaPierre
am 10 Nov. 2020
Bearbeitet: Cris LaPierre
am 10 Nov. 2020
+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')
userinput = "Afghanistan"
userinput2 = "Australia"
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 Kommentare
Cris LaPierre
am 16 Nov. 2020
That's not the same as what I shared. You must use strings (double quotes), not chars (single quotes) for this syntax to work.
Siehe auch
Kategorien
Mehr zu Biotech and Pharmaceutical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!