Clear Filters
Clear Filters

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

19 views (last 30 days)
I would like to be able to make a push button in my GUI that changes its text when it is clicked.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 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

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!