How do I make a push button that will change its text when I click on it?
15 ビュー (過去 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
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Migrate GUIDE Apps についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!