How to control a push button?
25 次查看(过去 30 天)
显示 更早的评论
hi guys, I have a push button and I want that when I click on it another push button will appear and if I click a second time so other push button appear etc.... I need your help because i'm really lost and I need to know how to do it thanks
0 个评论
回答(1 个)
Geoff Hayes
2014-7-9
I'll assume that you are using GUIDE to build your GUI.
Suppose that you have two push buttons named pushbutton1 and pushbutton2. When your GUI launches, only the first button is visible. To hide the second button, in your GUI's yourGuiName_OpeningFcn function, add the following code
set(handles.pushbutton2,'Visible','off');
yourGuiName is the name of your GUI. The above line of code hides the push button.
Now in the GUI editor, select the first push button and from the menu, select View --> View Callbacks --> Callback. The following code should appear in the MATLAB m file editor
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
In the body for this function, just add
set(handles.pushbutton2,'Visible','on');
Now run the GUI. You will observe that only the first push button is visible. Press it, and the second will appear.
To allow for another push button to appear on the second press of the first button (which I think is also part of your question), you will need to track the number of times that the button is pressed.
Let us assume that there is a third push button named pushbutton3. In yourGuiName_OpeningFcn function, modify the body so that it looks like
% Choose default command line output for untitled2
handles.output = hObject;
% initialize the counter for number of times pressed for button 1
handles.button1PressCount = 0;
% Update handles structure
guidata(hObject, handles);
set(handles.pushbutton2,'Visible','off');
set(handles.pushbutton3,'Visible','off');
In the above, we added some user-defined data to the handles object in the form of a counter of the number of times that the first push button has been pressed. We then save this data using the guidata(hObject, handles); call. Order is important here! We then hide the third push button.
In the callback to the first push button, modify its body as follows
% increment the counter for the press count for this button
handles.button1PressCount = handles.button1PressCount + 1;
% save the data
guidata(hObject, handles);
if handles.button1PressCount==1
set(handles.pushbutton2,'Visible','on');
elseif handles.button1PressCount==2
set(handles.pushbutton3,'Visible','on');
end
So we increment the counter for the number of times the button has been pressed and then save the data using guidata. The if/elseif is then used to determine which button to reveal.
Try the above and see what happens!
5 个评论
Geoff Hayes
2014-7-11
How would you allow the user to select which buttons to hide? What is the use case behind this?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!