How do I make a push button that will change its text when I click on it?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I would like to be able to make a push button in my GUI that changes its text when it is clicked.
Réponse acceptée
MathWorks Support Team
le 27 Juin 2009
In order to cyclically change the text string on the push button, you need to determine what the current 'String' property value is, and set it to the other value.
Below is an example of a push button that switches the push button's String between ''Pause" and ''Resume" :
function Test_Button(hObject, eventdata, handles, varargin)
%Create the GUI
Fig = figure;
% Create the Push button using uicontrol, using callback function push_Callback, initially the text displayed for the button is "Pause"
PushCtrl = uicontrol( Fig,'Style' ,'pushbutton',...
'String', 'Pause',...
'Position', [20 20 100 60],...
'Callback', @push_Callback);
% Create the handles structure
handles = guihandles(Fig);
handles.strings = {'Pause';'Resume'};
% Save the handles structure to the fig file
guidata(Fig, handles);
%%This function checks the current string of push button and changes it.
function varargout = push_Callback(h, eventdata)
% Get the handles structure
handles = guidata(h);
% Get the current string
str = get(h,'String');
% See which string the current string matches with
ind = find(strcmp(str,handles.strings));
% Switch to the other string
if ind == 1
set(h,'String',handles.strings{2});
else
set(h,'String',handles.strings{1});
end
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!