필터 지우기
필터 지우기

Using drop-down list selection to plot

조회 수: 36 (최근 30일)
Matt R
Matt R 2021년 1월 15일
댓글: Cris LaPierre 2021년 3월 10일
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

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 1월 15일
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
Nathaniel Johnston
Nathaniel Johnston 2021년 3월 10일
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 2021년 3월 10일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Mobile Fundamentals에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by