Making a GUI in MATLAB with Drop Down
3 次查看(过去 30 天)
显示 更早的评论
This is my MATLAB code:
x=[-80:5:0, 5:60]
hold on
%Rubber 1
f1=[5E-08 -8E-05 -0.0041 1.7746 120.380]
f2=[-3E-07 -0.0001 0.0044 1.7961 69.802]
f3=[-1E-06 -9E-05 0.01400 1.5669 31.916]
y1=polyval(f1,x)
y2=polyval(f2,x)
y3=polyval(f3,x)
plot(x,y1,'-.g')
plot(x,y2,'g')
plot(x,y3,'--g')
z=60
lv1=find(y1<=z,1,'last');
isx1=interp1(y1(lv1+[-1 1]),x(lv1+[-1 1]),z)
plot(isx1,z,'ro')
lv2=find(y2<=z,1,'last');
isx2=interp1(y2(lv2+[-1 1]),x(lv2+[-1 1]),z)
plot(isx2,z,'ro')
lv3 = find(y3<=z,1,'last');
isx3=interp1(y3(lv3+[-1 1]),x(lv3+[-1 1]),z)
plot(isx3,z,'ro')
hold off
grid on
legend('Location','northwest')
legend 'Upper' 'Median' 'Lower'
This is my GUI:
%--------------------------------------------------------
% GUI
%--------------------------------------------------------
%Main figure containing the uicontrols
my_figure=figure('Position', [500,200,500,250]);
%Dropdown for the user input
c = uicontrol('Parent',my_figure,'tag','force','Style','popupmenu');
c.Position = [20,100,100,80];
c.String = {'Rubber 1','Rubber 3','Rubber 3'};
%%Edit box displays the the Input
uicontrol('Parent',my_figure,'tag','force','Style','edit',...
'Position',[125,160,50,20])
%Edit box displays the result (Lower)
uicontrol('Parent',my_figure,'tag','result','Style','edit',...
'Position',[125,100,50,20])
%Edit box displays the result (Median)
uicontrol('Parent',my_figure,'tag','result','Style','edit',...
'Position',[200,100,50,20])
%Edit box displays the result (Upper)
uicontrol('Parent',my_figure,'tag','result','Style','edit',...
'Position',[275,100,50,20])
%Push button for execution of script StressInBeams_8
uicontrol('Parent',my_figure,'tag','run','Style','pushbutton',...
'Position',[20,100,50,20],'string','RUN',...
'callback','Rubber_Types')
%--------------------------------------------------------
What i want to do is be able to:
- Pick rubber type in the popup menu
- Change the impct force in first unicontrol
- Then press run
- Show the three results in the last three unicontrols
Can anybody help me?
2 个评论
Walter Roberson
2020-8-8
%Edit box displays the result (Lower)
uicontrol('Parent',my_figure,'tag','result','Style','edit',...
'Position',[125,100,50,20])
%Edit box displays the result (Median)
uicontrol('Parent',my_figure,'tag','result','Style','edit',...
'Position',[200,100,50,20])
You are not recording the handle of the uicontrol style edit, and you have given three of them the same tag. How do you intend to distinguish between them in your program?
回答(1 个)
Walter Roberson
2020-8-8
Generally speaking, when you create a GUI, you have multiple phases:
- Code that creates the GUI elements and sets all required Callback properties.
- Code that waits for something to happen. This is typically fairly simple code, and can consist of as little as returning to the command line, if you do not mind the user having access to the command line. For the kind of GUI that you are creating, MATLAB will take care of making the calls to the callback functions when the defined events occur, provided that the code that has control is permitting events to be checked for
- Callback code that validates combinations of inputs, potentially enabling more inputs as you go (e.g., it might not make sense to permit the user to enter the second input before the first one.) Eventually, through some combinations of actions, the user indicates that they are ready for the calculation to proceed
- Code (invoked from a callback) that does the calculation
- Code (invoked from a callback) that extracts results and formats them and updates the display.
It is common for the code that does the calculation and the code that updates the display, to all be combined into the callback for one of the input control events. In simple cases, you might just rely upon the fact that a particular input was changed to invoke the calculation, but in more complex cases, it is common for there to be some kind of pushbutton that indicates that the user wants to proceed to the calculation.
It is also not uncommon for the callback for proceeding with the calculation to be the routine that validates all of the inputs, with there being no callbacks on the individual inputs.
If you look at the code you have created, you have created the gui elements, but you have not set up any callbacks, and you have not set up any input validation, and you have not set up any way for the user to indicate that they want to proceed. You have also not put your calculation into a form that can be invoked from a callback, and you have not put in any code to extract results and send them to the display.
Because you have not currently recorded the handles of the ouput fields and you have not distiguished between them by using different tag fields or different UserData, then the code is going to have trouble figuring out which of the output fields to send what to.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!