Change pushbutton text in a GUI

22 ビュー (過去 30 日間)
Daniel LaCroix
Daniel LaCroix 2015 年 11 月 19 日
コメント済み: Daniel LaCroix 2015 年 11 月 19 日
I'm trying to make a GUI with two pushbuttons. After one button is pushed, I want to change the text on both pushbuttons. I've figured out how to change the text on the pushbutton that was clicked, but not the other one. This is my code:
function rainbow
r1=uicontrol('Style',...
'pushbutton',...
'String','Option 1',...
'Position',[10 350 100 30],...
'HandleVisibility','on',...
'Callback',@pushbutton_Callback);
r2=uicontrol('Style','pushbutton',...
'String','Option 2',...
'Position',[10 250 100 30],...
'HandleVisibility','on',...
'Callback',@pushbutton_Callback);
end
function pushbutton_Callback(hObject,callbackdata,handles)
disp(hObject.String)
hObject.String='Option 3';
end
I tried putting handles.r1.String='Option 4' within the callback, but that didn't do anything.

採用された回答

Michael Mancinelli
Michael Mancinelli 2015 年 11 月 19 日
First you should Tag your ui-buttons, so you can reference them within the guidata. Second you call pull the gui handles from within the callback.
See below:
function rainbow
clf;
r1=uicontrol('Style',...
'pushbutton',...
'String','Option 1',...
'Position',[10 350 100 30],...
'HandleVisibility','on',...
'Tag','Button1',...
'Callback',@pushbutton_Callback);
r2=uicontrol('Style','pushbutton',...
'String','Option 2',...
'Position',[10 250 100 30],...
'HandleVisibility','on',...
'Tag','Button2',...
'Callback',@pushbutton_Callback);
end
function pushbutton_Callback(hObject,callbackdata)
disp(hObject.String)
handles = guihandles(hObject);
handles.Button1.String = 'Button 1 string after any click';
handles.Button2.String = 'Button 2 string after any click';
end
  1 件のコメント
Daniel LaCroix
Daniel LaCroix 2015 年 11 月 19 日
Thanks, the tags were exactly what I was looking for.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDialog Boxes についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by