Making a GUI in MATLAB with Drop Down

조회 수: 6 (최근 30일)
Kenneth Bisgaard Cristensen
Kenneth Bisgaard Cristensen 2020년 8월 8일
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:
  1. Pick rubber type in the popup menu
  2. Change the impct force in first unicontrol
  3. Then press run
  4. Show the three results in the last three unicontrols
Can anybody help me?
  댓글 수: 2
Walter Roberson
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?
Kenneth Bisgaard Cristensen
Kenneth Bisgaard Cristensen 2020년 8월 8일
I don't really know, it's my first try at a GUI. But need it to show the impact temperature in the three curves when I change the impact force and press run.
But to be honest not entirely sure what I'm doing, so would appreciate some input to do this the correct way

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

답변 (1개)

Walter Roberson
Walter Roberson 2020년 8월 8일
Generally speaking, when you create a GUI, you have multiple phases:
  1. Code that creates the GUI elements and sets all required Callback properties.
  2. 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
  3. 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
  4. Code (invoked from a callback) that does the calculation
  5. 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.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by