Azzera filtri
Azzera filtri

app designer - dropdownmenu - plot values

37 visualizzazioni (ultimi 30 giorni)
alejandro paradiso
alejandro paradiso il 25 Set 2023
Risposto: Binaya il 17 Ott 2023
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 Commenti
Abderrahim. B
Abderrahim. B il 25 Set 2023
Hi!
Can you describe further what data you have, what are you trying to achieve and what final plot is like ?
Thanks,
alejandro paradiso
alejandro paradiso il 25 Set 2023
Hello, i've found a csv on kaggle about flights arriving to each eu country. i can get the result programmatically with live script
austriaCo2Idx=data.statename=="AUSTRIA"; %logical indexing
austriaCo2=data(austriaCo2Idx,:); % filtering
gscatter(austriaCo2.month,austriaCo2.co2,austriaCo2.year); %gscatter for quick preview
grid on
xlabel 'Month'
ylabel 'Total Flights'
title 'Austria'
austriaY=groupsummary(austriaCo2,"year","sum",["co2","totalflights"]); %grouping data per year
plot(austriaY.year,austriaY.sum_totalflights)
grid on
xlabel 'Year'
ylabel 'Total Flights'
title 'Austria'
xticks(2010:2023)
xlim([2010 2023])
austriaM=groupsummary(austriaCo2,"month","sum",["co2","totalflights"]); %grouping data per month
plot(austriaM.month,austriaM.sum_totalflights)
grid on
title 'Austria'
xlabel 'Month'
ylabel 'Total Flights'
xticks(1:12)
xlim([1 12])
what i would like to do is to make a webapp, using a dropdown menu listing all eu countries and then plotting the same graph as per script above according to user selection.

Accedi per commentare.

Risposte (1)

Binaya
Binaya il 17 Ott 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:
  1. Begin by creating the application and loading all the necessary data.
  2. Utilize the Component Library to add "Axes" and "Drop Down" components to your application's interface.
  3. 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.
  4. In the code section of the aforementioned "DropDownValueChangedFcn," utilize the "app.DropDown.Value" property to create conditional statements for the desired plots.
  5. Following the conditional statement, employ a plot command to visualize the required data within the "Axes" component.
  6. 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:
  1. Display Graphics in App Designer: https://www.mathworks.com/help/matlab/creating_guis/graphics-support-in-app-designer.html
  2. 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

Categorie

Scopri di più su Live Scripts and Functions in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by