app designer - dropdownmenu - plot values
7 views (last 30 days)
Show older comments
Good Morning to all,
before this quesiton, i've read & search a lot, but i never found a straight answer.
background: since i've made some ELT, now i have cleaned data and I want to create an app since that plot data from file. The drop down menu is made with the rows of my csv. I've put a button "load" that performs all preliminary task in order to get data formatted.
pts = spreadsheetImportOptions("NumVariables", 6);
% Specify sheet and range
opts.Sheet = "DATA";
opts.DataRange = "A2:F6838";
% Specify column names and types
opts.VariableNames = ["YEAR", "MONTH", "STATE_NAME", "STATE_CODE", "CO2_QTY_TONNES", "TF"];
opts.VariableTypes = ["double", "double", "categorical", "categorical", "double", "double"];
% Specify variable properties
opts = setvaropts(opts, ["STATE_NAME", "STATE_CODE"], "EmptyFieldRule", "auto");
% Import the data
app.data = readtable("C:\Users\Ale\Documents\MATLAB\Datasets\Flights\CO2_emissions_by_state.xlsx", opts, "UseExcel", false);
app.data.Properties.VariableNames(5) = "co2";
app.data.Properties.VariableNames(4) = "statecode";
app.data.Properties.VariableNames(3) = "statename";
app.data.Properties.VariableNames(2) = "month";
app.data.Properties.VariableNames(1) = "year";
app.data.Properties.VariableNames(6) = "totalflights";
clear opts
Then I've declared all properties (variables) i will need.
properties (Access = private)
ALBANIA
ARMENIA
AUSTRIA
BELGIUM
year
totalflights
sum_totalflights
data
statename
%===Albania
albaniaCo2Idx
albaniaCo2
albaniaY
% === Armenia
armeniaCo2Idx
armeniaCo2
armeniaY
% === Austria
austriaCo2Idx
austriaCo2
austriaY
%===== Belgium
belgiumCo2Idx
belgiumCo2
belgiumY
end
target: i want to plot data after changing values from dropdownmenu. I've tried several way to get there, but i can only plot the first value
function StateDropDown_Albania(app, event)
%===Albania
app.albaniaCo2Idx=app.data.statename=="ALBANIA";
app.albaniaCo2=app.data(app.albaniaCo2Idx,:);
app.albaniaY=groupsummary(app.albaniaCo2,"year","sum",["co2","totalflights"]);
app.year=app.albaniaY.year;
app.totalflights=app.albaniaY.sum_totalflights;
plot(app.UIAxes,app.year,app.totalflights)
grid on
end
% Callback function
function StateDropDown_Austria(app, event)
%===Austria
app.austriaCo2Idx=app.data.statename=="AUSTRIA";
app.austriaCo2=app.data(app.austriaCo2Idx,:);
app.austriaY=groupsummary(app.austriaCo2,"year","sum",["co2","totalflights"]);
app.year=app.austriaY.year;
app.totalflights=app.austriaY.sum_totalflights;
plot(app.UIAxes,app.year,app.totalflights)
end
and so on...
after running, i can i only get the plot for one variable, but i would like to have a plot for all EU countries
any guess, indication will be appreciated
2 Comments
Abderrahim. B
on 25 Sep 2023
Hi!
Can you describe further what data you have, what are you trying to achieve and what final plot is like ?
Thanks,
Answers (1)
Binaya
on 17 Oct 2023
Hi Alejandro,
As per my understanding, you are interested in creating an app that allows you to plot different data from an Excel file based on the selected option in a drop-down menu.
Please follow the steps given below as a possible solution to your query:
- Begin by creating the application and loading all the necessary data.
- Utilize the Component Library to add "Axes" and "Drop Down" components to your application's interface.
- Within the "Drop Down" component, incorporate a callback function under the "ValueChangedFcn" category. a. This callback function will be triggered whenever an option is selected from the drop-down menu.
- In the code section of the aforementioned "DropDownValueChangedFcn," utilize the "app.DropDown.Value" property to create conditional statements for the desired plots.
- Following the conditional statement, employ a plot command to visualize the required data within the "Axes" component.
- For instance, you can use the following example syntax
plot(app.UIAxes,xdata,ydata);
where "app.UIAxes" specifies the axes where the data will be plotted.
Please refer to below MathWorks Documentation for more details:
- Display Graphics in App Designer: https://www.mathworks.com/help/matlab/creating_guis/graphics-support-in-app-designer.html
- Callbacks in App Designer: https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-gui-in-app-designer.html
I hope this helps.
Regards
Binaya
0 Comments
See Also
Categories
Find more on Live Scripts and Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!