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 commentaires

Mario Malic
Mario Malic le 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.
Kelsey Pettrone
Kelsey Pettrone le 10 Nov 2020
Do you know how i could make it so the slider only uses integers?

Connectez-vous pour commenter.

Réponses (1)

Cris LaPierre
Cris LaPierre le 10 Nov 2020
Modifié(e) : Cris LaPierre le 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')
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 commentaires

Note that you can further simplify your code by accessing the data in your table directly (no intermediate variables). However, this might come at the cost of readability. For example, recreating the second plot could look like this.
data = readtable('RiskFactorAnalysis.csv');
userinput = "Afghanistan";
userinput2 = "Australia";
useryear = 2003;
ind = strcmp(data.Entity,userinput);
% further simplify by extacting data all together.
figure
ax2 = gca;
grid(ax2,'on')
% note the curly braces for extracting data from multiple table variables below
bar(ax2,data.Year(ind),data{ind,5:8},'grouped');
xlabel(ax2,'year')
ylabel(ax2,'deaths')
title(ax2,userinput)
legend(ax2,'Obesity','Drug','Alcohol','Smoking')
Kelsey Pettrone
Kelsey Pettrone le 10 Nov 2020
How would I do this if i want the user to pick the year? I'm still so confused. and what does ax2 = gca mean?
"I made some modifications to your data to allow it to run as a script here (creating figures and axes handles)".
Keep what you had for these in your code.
userinput = app.CountryDropDown.Value;
userinput2 = app.CountryDropDown_2.Value;
useryear = app.Slideryear.Value;
...
grid(app.Axes2,'on')
...
Kelsey Pettrone
Kelsey Pettrone le 10 Nov 2020
thank you so much! do you know how to make the slider only use integers in the app portion. Do i have to add a callback for it? i cant figure this out
Cris LaPierre
Cris LaPierre le 10 Nov 2020
There is no setting to have a slider return discrete values, but it can be accomplished programmatically. See this post for a detailed example.
Kelsey Pettrone
Kelsey Pettrone le 10 Nov 2020
so should everything that says ax3 say app.Axes3?
I'm so sorry about all these questions thank you so much for responding. I really appreciate it.
Yes. Remove
figure
ax3 = gca;
and then replace anywhere that says ax3 with app.Axes3.
Repeat for ax1 and ax2.
Kelsey Pettrone
Kelsey Pettrone le 10 Nov 2020
i'm really sorry again but now its saying "Unable to resolve the name data.Entity.
Error in deliverable1 (line 2)
ind = strcmp(data.Entity,userinput);"
Cris LaPierre
Cris LaPierre le 10 Nov 2020
Open data in your variable editor and confirm the variable name.
The debugger is your friend. I suggest learning how to use it.
Kelsey Pettrone
Kelsey Pettrone le 12 Nov 2020
There is an error when i try to change the year on the slider. Do you know whats happening? I've tried to get this working for 3 days
Cris LaPierre
Cris LaPierre le 12 Nov 2020
Modifié(e) : Cris LaPierre le 13 Nov 2020
What is the error message you are getting? What is the code you are using that relies on the slider value? How do you select the data for a single year?
Error using bar (line 171)
When X is a scalar, Y must be a vector.
Error in deliverable1 (line 40)
bar(app.Axes3,useryear,bpcombined(indY2,:),'grouped'); % Note that I am only plotting 1 row of bpcombined here
it works fine on the year 1990 but when i try to change it it doesnt work. the app and code are attached
function deliverable1(app)
data = readtable('RiskFactorAnalysis (1).csv');
userinput = app.CountryDropDown.Value;
userinput2 = app.CountryDropDown_2.Value;
useryear = app.Slideryear.Value
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
grid(app.Axes,'on')
plot(app.Axes,year,deaths,year2,deaths2); % plot deaths2 against year2, not year
xlabel(app.Axes,'year')
ylabel(app.Axes,'deaths per 100000')
legend(app.Axes,userinput,userinput2)
grid(app.Axes2,'on')
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')
indY2 = year2==useryear; % again, use year2, not year
grid(app.Axes3,'on')
bpcombined = [obesity2, Drug2, Alcohol2, smoking2];
bar(app.Axes3,useryear,bpcombined(indY2,:),'grouped'); % Note that I am only plotting 1 row of bpcombined here
xlabel(app.Axes3,'year')
ylabel(app.Axes3,'deaths')
title(app.Axes3,userinput2)
legend(app.Axes3, 'Obesity', 'Drug', 'Alcohol', 'Smoking')
Kelsey Pettrone
Kelsey Pettrone le 13 Nov 2020
I'm Also trying to put them on the same graph now and thats not working.
Cris LaPierre
Cris LaPierre le 13 Nov 2020
What version of MATLAB are you using?
Kelsey Pettrone
Kelsey Pettrone le 15 Nov 2020
R2020b, I got them on the same graph now.
Kelsey Pettrone
Kelsey Pettrone le 15 Nov 2020
do you know how i would get the legend to say Austrailia smoking instead of just smoking?
Modify the names used in your legend command.
You can combine a variable and a string.
userinput2 = "Australia";
bar(2003,[50 5 25 76],'grouped')
title(userinput2)
legend(userinput2 + " " + ["Obesity" "Drug" "Alcohol" "Smoking"])
Kelsey Pettrone
Kelsey Pettrone le 16 Nov 2020
Modifié(e) : Cris LaPierre le 16 Nov 2020
legend(app.Axes2,(userinput +' '+['Obesity','Drug','Alcohol','Smoking']),...
(userinput2,+' '['obesity','drug','alcohol','smoking']))
i tried it its not working
Cris LaPierre
Cris LaPierre le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by