필터 지우기
필터 지우기

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

조회 수: 14 (최근 30일)
I would like to be able to make a push button in my GUI that changes its text when it is clicked.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2009년 6월 27일
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개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by