My app isnt working right
古いコメントを表示
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
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
2020 年 11 月 10 日
回答 (1 件)
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')
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 件のコメント
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
2020 年 11 月 10 日
Cris LaPierre
2020 年 11 月 10 日
"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
2020 年 11 月 10 日
Cris LaPierre
2020 年 11 月 10 日
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
2020 年 11 月 10 日
Cris LaPierre
2020 年 11 月 10 日
Yes. Remove
figure
ax3 = gca;
and then replace anywhere that says ax3 with app.Axes3.
Repeat for ax1 and ax2.
Kelsey Pettrone
2020 年 11 月 10 日
Cris LaPierre
2020 年 11 月 10 日
Open data in your variable editor and confirm the variable name.
Kelsey Pettrone
2020 年 11 月 12 日
Cris LaPierre
2020 年 11 月 12 日
編集済み: Cris LaPierre
2020 年 11 月 13 日
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?
Kelsey Pettrone
2020 年 11 月 12 日
Kelsey Pettrone
2020 年 11 月 13 日
Cris LaPierre
2020 年 11 月 13 日
What version of MATLAB are you using?
Kelsey Pettrone
2020 年 11 月 15 日
Kelsey Pettrone
2020 年 11 月 15 日
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
2020 年 11 月 16 日
編集済み: Cris LaPierre
2020 年 11 月 16 日
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.
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




