Using drop-down list selection to plot

19 visualizzazioni (ultimi 30 giorni)
I am building an app that should do a few simple things:
  • imports large csv files. The file is a table with datetime and approx. double variables. 25 in total.
  • Plot chosen variables based on drop-down lists on a graph
  • Export the graph to pdf
The problem I'm having is with the Drop-down list. I can populate the list with the variable names, however, I am struggling to link the selected variable to its column in the imported file and then plot the data. For reference, these variable can change with each import and therefore can't be fixed - I need them to be read in. I don't quite understand the syntax to relate the DropDown.Item.Value to the matrix contents.
I'm using version R2020b and I have tried for a while with other posts in the forum and documentation with little success.
I've posted some of the code below of where it works up to.
Can anyone help me out?
[file] = uigetfile('*.csv');
if isequal(file,0)
msgbox('Please input a CSV file')
else
app.SD = importdatav7(file);
isequal(file, 1)
opts = detectImportOptions(file, "NumHeaderLines", 24, "VariableNamesLine", 1);
% Create Variable1DropDownLabel
app.dd1Label = uilabel(app.LeftPanel);
app.dd1Label.HorizontalAlignment = 'right';
app.dd1Label.Position = [47 275 71 22];
app.dd1Label.Text = 'Variable 1';
% Create Variable1DropDown
app.dd1 = uidropdown(app.LeftPanel);
app.dd1.DropDownOpeningFcn = createCallbackFcn(app, @Variable1DropDownOpening, true);
app.dd1.Position = [136 275 100 22];
% Create Variable2DropDownLabel
app.dd2Label = uilabel(app.LeftPanel);
app.dd2Label.HorizontalAlignment = 'right';
app.dd2Label.Position = [60 235 58 22];
app.Vdd2Label.Text = 'Variable 2';
% Create Variable2DropDown
app.dd2 = uidropdown(app.LeftPanel);
app.dd2.Position = [137 235 99 22];
% Create Variable3DropDownLabel
app.dd3Label = uilabel(app.LeftPanel);
app.dd3Label.HorizontalAlignment = 'right';
app.dd3Label.Position = [63 192 58 22];
app.dd3Label.Text = 'Variable 3';
% Create Variable3DropDown
app.dd3 = uidropdown(app.LeftPanel);
app.dd3.Position = [136 192 100 22];
labels = opts.VariableNames;
app.dd1.Items = labels;
app.dd2.Items = labels;
app.dd3.Items = labels;
msgbox("Import Successful!")
end

Risposta accettata

Cris LaPierre
Cris LaPierre il 15 Gen 2021
There is no DropDown.Item.Value property. See this page for more.
There is a DropDown.Items property. This is everything the drop down list contains.
There is a DropDown.Value item. This is the text value of the currently selected iteam. In your case, it is the selected variable name.
My recommended way to approach this would be to read in your csv file as a table (see Advantages of Using Tables). You can then use the variable name to directly index the corresponding column in the table. See the Access Data in Tables page for more.
  5 Commenti
Nathaniel Johnston
Nathaniel Johnston il 10 Mar 2021
This question may relate to the issue I am experiencing too I am currently trying to get my drop down list to work for filtering/plotting on my uiaxes for example I select weight and want to use the drop-down list to filter based on male or female would this solution above work ?
Cris LaPierre
Cris LaPierre il 10 Mar 2021
It would depend on how your data is arranged. This approach assumes the selected values are in different columns of the table.
You can use this approach to capture the filter criteria, but you will have an extra step of applying those criteria to your data to select what should be plotted.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by