Borrar filtros
Borrar filtros

How do I make a push button that will change its text when I click on it?

19 visualizaciones (últimos 30 días)
I would like to be able to make a push button in my GUI that changes its text when it is clicked.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 27 de Jun. de 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

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by