how to get a value from a drop down menu in matlab

54 次查看(过去 30 天)
Im trying to programmatically build a UI, im doing it this way because I have some complicated surfaces that I want to calculate and I want to do it for thousands of files. The matlab code do this is done, but I have a few pop up messages and input boxes that work, its clunky to say the least so im trying to clean it up with uifigure. Right now in my regular matlab code that I have been building, an input box pops up that asks if you want to and how to save data (i.e. either as a csv or txt file), if you want to move data and what type of surface you want to build. If a "calculated surface" if chosen, another input box pops up and asks if you want a surface with lowess smoothing or cubic interpolation, this only pops up if you choose the right option.
In uifigure im trying to duplicate this, I have a drop down that asks what kind of surface you want to create, if a "calculated surface" is chosen, I want another drop down menu to show up asking what type of calculated surface you want to make. This is where im having trouble and I need to figure it out before I can add in my other calculations. When I select a drop down option, dd1.Value is not changing, it stays at 0, in the switch function at the bottom im trying to say if dd1.Value = 2 then dd3.Visible = 'on'. When I run it, everything works fine however dd1.Value isnt changing depnding on user selection and im not sure where im going wrong or what I need to add. Any advice is appreciated. Thanks!
close all
fig = uifigure("Name", 'Single Beam Processing', 'Position', [30,75,1450,750]);
panel1 = uipanel(fig, "Position", [50,350,200,350]);
panel1.Title = "Program Setup";
panel1.TitlePosition = 'centertop';
panel1.FontSize = 14;
txtbox1 = uieditfield(panel1,"Text", "Position", [10,290,180,30], "Value", "Operator Name");
dd1 = uidropdown(panel1, 'Position', [10,250,180,30]);
dd1.Items = ["Choose Surface Type", "Depth", "Calculated Surface", "Fitted Plane", "Corrected Points", "None"];
dd1.ItemsData = [0 1 2 3 4 5];
dd1.Visible = 'on';
dd1value = dd1.Value;
dd2 = uidropdown(panel1, 'Position', [10,210,180,30]);
dd2.Items = ["Choose Save Options", "Save as CSV", "Save as txt"];
dd2.ItemsData = [0 1 2];
dd1.Visible = 'on';
dd2value = dd2.Value;
dd3 = uidropdown(panel1, 'Position', [10,170,180,30]);
dd3.Items = ["Choose Smoothing Type", "Lowess", "Cubic Interpolation"];
dd3.ItemsData = [0 1 2];
dd3.Visible = 'off';
switch dd1value
case 2
dd3.Visible = 'on';
otherwise
dd3.Visible = 'off';
end

采纳的回答

Voss
Voss 2024-3-20
编辑:Voss 2024-3-21
That line executes immediately after dd1 is created/set-up, so dd1value is whatever Value dd1 has initially. That is to say, that line executes before you have a chance to make a selection in dd1.
To be able to make selections and then use them for subsequent calculations, you need to make the program wait for the user. In a programmatic dialog like this, that typically means calling uiwait.
As for the question of having the selection made in dd1 change the visibility of dd3, you need to give dd1 a ValueChangedFcn, which executes when dd1's Value changes. In that function, you'll set the visibility of dd2 appropriately.
See the below code for those modifications. I also added a button the user clicks to signal that the selections are done, so the program should prepare the outputs and close the figure.
function [name,dd1_option,dd2_option,dd3_option] = Surface_Options()
fig = uifigure("Name", 'Single Beam Processing', 'Position', [30,75,1450,750]);
panel1 = uipanel(fig, "Position", [50,350,200,350]);
panel1.Title = "Program Setup";
panel1.TitlePosition = 'centertop';
panel1.FontSize = 14;
txtbox1 = uieditfield(panel1,"Text", "Position", [10,290,180,30], "Value", "Operator Name");
dd1 = uidropdown(panel1, 'Position', [10,250,180,30], 'ValueChangedFcn', @vcf_dd1);
dd1.Items = ["Choose Surface Type", "Depth", "Calculated Surface", "Fitted Plane", "Corrected Points", "None"];
dd1.ItemsData = [0 1 2 3 4 5];
dd1.Visible = 'on';
dd2 = uidropdown(panel1, 'Position', [10,210,180,30]);
dd2.Items = ["Choose Save Options", "Save as CSV", "Save as txt"];
dd2.ItemsData = [0 1 2];
dd1.Visible = 'on';
dd3 = uidropdown(panel1, 'Position', [10,170,180,30]);
dd3.Items = ["Choose Smoothing Type", "Lowess", "Cubic Interpolation"];
dd3.ItemsData = [0 1 2];
dd3.Visible = 'off';
uibutton(fig,'Position',[20 10 65 22],'Text','OK','ButtonPushedFcn',@bpf_btn);
% initialize outputs
name = txtbox1.Value;
dd1_option = dd1.Value;
dd2_option = dd2.Value;
dd3_option = dd3.Value;
% don't return until selections have been made and fig is closed
uiwait(fig);
function vcf_dd1(~,~)
switch dd1.Value
case 2
dd3.Visible = 'on';
otherwise
dd3.Visible = 'off';
end
end
function bpf_btn(~,~)
% update outputs
name = txtbox1.Value;
dd1_option = dd1.Value;
dd2_option = dd2.Value;
dd3_option = dd3.Value;
% close the figure
close(fig);
end
end
  6 个评论
Bradley
Bradley 2024-3-21
Thanky ou again for the detailed response, it was incredibly helpful. I was able to get the calculations I want to run by adding them into the bpf_btn function. Once the button is clicked it runs the calculations and plots my data. Thanks again!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by